Products CRUD

Everything is the same as creating CRUD for Brands except few things. We will only discuss NEW things here.

All other things (Adding controller, routes, etc are the same as explained in Brands CRUD tutorial)

Database migration for products table:

php artisan make:model Product -m
public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();

        $table->string('image')->nullable();
        $table->string('name')->unique();
        $table->foreignId('brand_id');
        $table->decimal('price');
        $table->boolean('is_active')->default(true);

        SpeedAdminHelpers::createdByUpdatedByMigrations($table);

        $table->timestamps();
    });
}

Database migration for "category_product" table for many_to_many relation between products and categories

Add relations in Product Model

Add getGridQuery($request) function to product model

As shown in the image above, in the products grid (datatable) we want to show tje following columns:

  • Product name

  • Image

  • Brand name

  • Categories names

  • Price

  • Categories count

  • Active

We also want to make sure that we can search products by brand_name or categories names. To do this, we need to modify our query. Our getGridQuery() function will look like following.

Add Grid Columns

Add form fields

All fields are the same as shown in Brands CRUD tutorial except for the following two fields (see image below):

  • Select field for selecting Brand

  • Select field for selecting multiple categories

Last updated