laravel商品模块表的创建添加商品api

554 阅读1分钟

这是我参与8月更文挑战的第28天,活动详情查看:8月更文挑战

一、商品数据表

1.1 创建商品数据表

使用命令php artisan make:model Good -m 创建商品模型、迁移文件: 在这里插入图片描述

1.2 商品数据表创建

database/migrations/2021_08_17_150039_create_goods_table.php中写入商品表结构:

Schema::create('goods', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->comment('创建者');
            $table->integer('category_id')->comment('分类');
            $table->string('description')->comment('描述');
            $table->integer('price')->comment('价格');
            $table->integer('stock')->comment('库存');
            $table->string('cover')->comment('封面图');
            $table->json('pics')->comment('小图集');
            $table->tinyInteger('is_on')->default(0)->comment('是否上架: 0不上架,1上架');
            $table->tinyInteger('is_recommend')->default(0)->comment('是否推荐:0不推荐,1推荐');
            $table->text('details')->comment('详情');
            $table->timestamps();
        });

在这里插入图片描述


执行迁移文件命令:php artisan migrate 在这里插入图片描述 在这里插入图片描述

二、商品管理

2.1 创建商品控制器

创建商品控制器命令: php artisan make:controller Admin/GoodsController --api 在这里插入图片描述 在这里插入图片描述

2.2 创建路由

routes/admin.php中写入:

            /*
             * 商品管理
             */
            // 是否上架
            $api->patch('goods/{good}/on', [GoodsController::class, 'isOn']);

            // 是否推荐
            $api->patch('goods/{good}/recommend', [GoodsController::class, 'isRecommend']);

            // 商品管理资源路由
            $api->resource('goods', GoodsController::class, [
                'except' => ['destroy']
            ]);

在这里插入图片描述


2.3 商品添加api

1、创建表单验证

由于商品字段较多,我们单独写一个文件进行表单验证。 运行命令php artisan make:request Admin/GoodsRequest 继承我们写的基础验证: 在这里插入图片描述


写入验证规则:

<?php

namespace App\Http\Requests\Admin;
use App\Http\Requests\BaseRequest;

class GoodsRequest extends BaseRequest
{
    /**
     * 验证
     */
    public function rules()
    {
        return [
            'category_id' => 'required',
            'description' => 'required|max:255',
            'price' => 'required|min:0',
            'stock' => 'required|min:0',
            'cover' => 'required',
            'pics' => 'required|array',
            'details' => 'required',
        ];
    }
    // 信息重写
    public function messages() {
        return [
            'category_id.required' => '分类不能为空',
            'description.required' => '描述不能为空',
            'description.max' => '长度不能超过255',
            'price.required' => '价格不能为空',
            'stock.required' => '库存不能为空',
            'stock.min' => '库存最小为0',
            'cover.required' => '封面图不能为空',
            'pics.required' => '小图集不能为空',
            'pics.array' => '小图集为数组',
            'details.required' => '详情不能为空',
        ];
    }
}

在这里插入图片描述


2、商品添加控制器

    /**
     * 添加商品
     */
    public function store(GoodsRequest $request)
    {
        $user_id = auth('api')->id();
        $request->offsetSet('user_id', $user_id);
        Good::create($request->all());
        return $this->response->created();
    }

在这里插入图片描述

3、增加可允许添加的字段以及类型转换

Models/Good.php中写入:

    // 可批量赋值的字段
    protected $fillable = ['user_id', 'category_id', 'description', 'price', 'stock', 'cover', 'pics', 'is_on', 'is_recommend', 'details'];

    // 强制转换的属性
    protected $casts = [
        'pics' => 'array',
    ];

在这里插入图片描述 好的,接着我们去测试: 在这里插入图片描述 在这里插入图片描述 可以看见商品表中已经插入了一条我们刚刚创建的数据。

在学习的php的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。