Appearance
任务1:新闻资讯数据库生成
重点学习,解决一对多,自带父,父带子的接口. load with
php
public function index() {
return $this->success(
data: CatResource::collection(Cat::orderBy('order', 'asc')->with('subs.products')->latest()->get())
);
}
public function show(Cat $cat) {
$data = new CatResource($cat->load('subs'));
return $this->success(data: $data);
}#1、生成新闻模块相关模块
php artisan make:model Info -a// 生成的文件
INFO Model [app/Models/Info.php] created successfully.
INFO Factory [database/factories/InfoFactory.php] created successfully.
INFO Migration [database/migrations/2024_01_24_105246_create_infos_table.php] created successfully.
INFO Seeder [database/seeders/InfoSeeder.php] created successfully.
INFO Request [app/Http/Requests/StoreInfoRequest.php] created successfully.
INFO Request [app/Http/Requests/UpdateInfoRequest.php] created successfully.
INFO Controller [app/Http/Controllers/InfoController.php] created successfully.
INFO Policy [app/Policies/InfoPolicy.php] created successfully.
#2、新闻资讯database生成,title,desc,preview,video
流程
table->factory->fake->seeder
cp/database/migrations/2024_01_24_105246_create_infos_table.php
public function up(): void{
Schema::create('infos', function (Blueprint $table) {
$table->id();
$table->string('title')->unique()->comment('新闻标题');
$table->string('preview')->nullable()->comment('新闻图');
$table->text('content')->comment('新闻内容');
$table->timestamps();
});
}php artisan migrate:refresh --seedcp/database/factories/InfoFactory.php
public function definition(): array{
return [
'title'=> fake()->sentence(),
'content'=>fake()->paragraph(),
'preview'=>fake()->imageUrl(),
];
}class InfoSeeder extends Seeder
{
public function run(): void{
Info::factory(10)->create();
}
}class DatabaseSeeder extends Seeder{
public function run(): void{
...代码
$this->call([
InfoSeeder::class
]);
}
}php artisan migrate:refresh --seedphp artisan make:resource InfoResourcephp artisan ide-helper:models任务2:新闻资讯的crud #1、新闻资讯的crud
cp/app/Http/Requests/UpdateInfoRequest.php
public function authorize(): bool{return true;} 修改时排除本身 Rule::unique('infos','title')->ignore($this->route('info')->id)
php
updateRequest
Rule::unique('infos','title')->ignore($this->route('info')->id)class Info extends Model{
use HasFactory;
protected $fillable = ['title', 'preview', 'content'];
}php
public function __construct()
{
$this->middleware('auth:sanctum')->only('update','destroy');
}
public function index()
{
//
return UserResource::collection(User::latest()->paginate());
}
public function show(User $user)
{
//
return $this->success(data:new UserResource($user));
}
public function update(Request $request, User $user)
{
//
$this->authorize('update',$user);
$user->fill($request->input())->save();
return $this->success(message: '修改成功',data:['user'=>new UserResource($user)]);
}
public function destroy(User $user)
{
//
$this->authorize('delete',$user);
$user->delete();
return $this->success(message: '删除成功');
}ps: $this->authorize('delete',$user); 不是destory方法名,是table->delete() 方法
任务3:在api工具中调试新闻资讯各接口
产品大类,一对多
第4章-后端-产品大类的相关操作
任务1:产品大类的crud
#1 数据库的建立
CatFactory
'title'=>fake()->unique()->word()#2 产品大类的crud
$table->unsignedInteger('order')->default(0)->commont('排序')index: CatRerouce::collection(Cat::orderBy('order','asc')->latest())
show: new CatResource($cat)
store: $cat->fill($request->input())-save();
delete: $cat->delete();任务2产品大类的Api检测 #1 修正一个错误,numeric
Request
[
'title'=>['required',Rule::unique('cats','title')->ignore($this->route('cat')->id)],
'order'=>['numeric']
]ps: numeric
#2 产品大类Api的检测
第5章-后端-产品类型的相关操作
任务1:产品类型的crud #1 几种模型关联的解释:一对一,一对多,多对多。
// 一对多
$table->foreignId('cat_id')->constrainedd()->onDelete('cascade')SubFactory 模拟数据
'cat_id'=>Cat::InRandomOrder()->first()->id#2 数据库的建立 #3 产品大类的crud
public function index() {
return $this->success(data: CatResource::collection(Cat::orderBy('order', 'asc')->with('subs.products')->latest()->get()));
}
public function show(Cat $cat) {
$data = new CatResource($cat->load('subs'));
return $this->success(data: $data);
}任务2:产品类型的crud2 #1 cat新增preview字段,sub新增preview字段 #2 数据库的建立 #3 产品类型的crud
任务2产品大类的Api检测 #1 修正一个错误,numeric #2 产品大类Api的检测
第6章-后端-产品的相关操作
任务1:产品数据库的建立 #1 通过数据迁移创建
任务1:产品数据库的建立 #1 产品的crud
任务3:产品接口在Api工具检测 #1 产品接口在Api工具检测
第7章
任务1:帖子的crud
第8章-后端-评论的相关操作
任务1:评论数据库的建立 #1 通过数据迁移创建,多态关系的建立
任务2:评论模型中关联关系的实现 #1 一些关联关系的实现
任务3:评论业务逻辑的实现 #1 创建模型帮助函数 info =>Info topic => Topic #2 控制器中实现业务逻辑
任务4:Api工具调试接口 #1 Api工具调试接口
第9章-后端-上传文件接口
任务1:上传文件接口 #1 创建控制器 php artisan make:controller AttachmentController; #2 创建UploadService
#3 安装裁切包 composer require spatie/image; #4 创建软连接 php artisan storage:link public_path('attachments') => storage_path('app/attachments'),
任务1:上传文件接口 #1 创建控制器 php artisan make:controller AttachmentController; #2 创建UploadService
#3 安装裁切包 composer require spatie/image; #4 创建软连接 php artisan storage:link public_path('attachments') => storage_path('app/attachments'),
