This is the 28th day of my participation in the August Text Challenge.More challenges in August

I. Commodity data sheet

1.1 Create commodity data tables

Using the commandphp artisan make:model Good -mCreate product model, migrate file:

1.2 Creation of commodity data table

Add the product table structure to database/migrations/2021_08_17_150039_create_goods_table.php:

Schema::create('goods'.function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->comment('Creator');
            $table->integer('category_id')->comment('classification');
            $table->string('description')->comment('description');
            $table->integer('price')->comment('price');
            $table->integer('stock')->comment('inventory');
            $table->string('cover')->comment('Cover');
            $table->json('pics')->comment('Little Atlas');
            $table->tinyInteger('is_on')->default(0)->comment('Stocked or not: 0 not stocked, 1 stocked');
            $table->tinyInteger('is_recommend')->default(0)->comment('Recommended: 0 not recommended, 1 recommended');
            $table->text('details')->comment('details');
            $table->timestamps();
        });
Copy the code


Execute the file migration command:php artisan migrate

Second, commodity management

2.1 Creating a Commodity Controller

Create goods controller command:php artisan make:controller Admin/GoodsController --api

2.2 Creating a Route

Add routes/admin.php:

            /* * merchandise management */
            // Whether it is available
            $api->patch('goods/{good}/on', [GoodsController::class, 'isOn']);

            // Whether recommended
            $api->patch('goods/{good}/recommend', [GoodsController::class, 'isRecommend']);

            // Commodity management resource routing
            $api->resource('goods', GoodsController::class, [
                'except'= > ['destroy']]);Copy the code


2.3 API for adding goods

1. Create form validation

Because of the large number of commodity fields, we write a separate file for form verification. Run the commandphp artisan make:request Admin/GoodsRequest:Inherit the basic validation we wrote:


Write validation rule:


      

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

class GoodsRequest extends BaseRequest
{
    /** * verify */
    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',]; }// Message overwrite
    public function messages() {
        return [
            'category_id.required'= >'Classification cannot be empty'.'description.required'= >'Description cannot be empty'.'description.max'= >'Length cannot exceed 255'.'price.required'= >'Price cannot be empty'.'stock.required'= >'Inventory cannot be empty'.'stock.min'= >'Inventory minimum 0'.'cover.required'= >'Cover image cannot be empty'.'pics.required'= >'Small atlas cannot be empty'.'pics.array'= >'Small graph set is an array'.'details.required'= >'Details cannot be empty',]; }}Copy the code


2. Add controllers to goods

    /** * add item */
    public function store(GoodsRequest $request)
    {
        $user_id = auth('api')->id();
        $request->offsetSet('user_id'.$user_id);
        Good::create($request->all());
        return $this->response->created();
    }
Copy the code

3. Add allowable fields and type conversions

In Models/ good.php, write:

    // Batch assignment fields
    protected $fillable = ['user_id'.'category_id'.'description'.'price'.'stock'.'cover'.'pics'.'is_on'.'is_recommend'.'details'];

    // Cast attributes
    protected $casts = [
        'pics'= >'array',];Copy the code

Ok, then let’s test: You can see that the item table has been inserted with the data we just created.

If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.