Laravel E-Commerce Application Development – Categories Section Part 1
Laravel E-Commerce Application Development ( 27 Lessons )
In this course, you’ll learn how to create an E-Commerce Website from scratch in Laravel. The process has never been easier I’ll take you from the very beginning stages of setting up Laravel till the last steps of adding products to the cart. If you’ve good understanding & experience in PHP & MySQL then this course is for you.
see full series- Laravel E-Commerce Application Development – Introduction
- Laravel E-Commerce Application Development – Initial Project Setup
- Laravel E-Commerce Application Development – Assets Setup Using Laravel Mix
- Laravel E-Commerce Application Development – Admin Model and Migration
- Laravel E-Commerce Application Development – Backend Admin Authentication
- Laravel E-Commerce Application Development – Base Controller and Repository
- Laravel E-Commerce Application Development – Settings Section Part 1
- Laravel E-Commerce Application Development – Settings Section Part 2
- Laravel E-Commerce Application Development – Categories Section Part 1
- Laravel E-Commerce Application Development – Categories Section Part 2
- Laravel E-Commerce Application Development – Attributes Section Part 1
- Laravel E-Commerce Application Development – Attributes Section Part 2
- Laravel E-Commerce Application Development – Attributes Section Part 3
- Laravel E-Commerce Application Development – Brands Section
- Laravel E-Commerce Application Development – Products Section Part 1
- Laravel E-Commerce Application Development – Products Section Part 2
- Laravel E-Commerce Application Development – Products Section Part 3
- Laravel E-Commerce Application Development – Products Section Part 4
- Laravel E-Commerce Application Development – Frontend Login & Registration
- Laravel E-Commerce Application Development – Categories Navigation
- Laravel E-Commerce Application Development – Catalog Listing
- Laravel E-Commerce Application Development – Product Details Page
- Laravel E-Commerce Application Development – Shopping Cart
- Laravel E-Commerce Application Development – Checkout
- Laravel E-Commerce Application Development – Payment Processing
- Laravel E-Commerce Application Development – Order Management
- Laravel E-Commerce Application Development – Wrap Up
This is the eighth part of the Laravel E-Commerce Application Development series, in this part will add the model, migrations and model factory for categories section.
I assume you should have the e-commerce application project on your machine or you can grab it from Laravel E-Commerce Application repository, we will start from where we left it in the last part.
In this post, we will create a category model, migration, seed, and factor to our application.
So let’s start some coding.
Creating Category Model and Migration
To save our product categories to the database, we will need to create model and migration which will create a table in the database. Open your command line terminal and run the below command to generate the model and migration for categories.
php artisan make:model Models\Category -m
This command will generate a Category model in app/Models
folder with migration file in database/migrations
folder.
Open your migration file and update with the below one.
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('slug')->unique(); $table->text('description')->nullable(); $table->unsignedInteger('parent_id')->default(1)->nullable(); $table->boolean('featured')->default(0); $table->boolean('menu')->default(1); $table->string('image')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } }
As you can see we are adding various columns for our categories
table. We are adding name
, slug
, description
and image
columns which you might have in any model of your application.
We are also adding the parent_id
column, where we will store the parent category id to make a nested tree of categories. Next, we have added a featured
column with boolean type, based on this, we will show the featured categories on our homepage. The menu
field will provide us some control to show or hide a category in the main navigation of our website.
Now open, your Category model and define the $fillable
and $cast
properties like below.
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Category extends Model { protected $table = 'categories'; protected $fillable = [ 'name', 'slug', 'description', 'parent_id', 'featured', 'menu', 'image' ]; protected $casts = [ 'parent_id' => 'integer', 'featured' => 'boolean', 'menu' => 'boolean' ]; }
Creating Category Seed and Factory
Next, we will add the database seed class and a model factory class for quickly generating dummy data. Run the below two commands in the terminal to generate seed and factory.
php artisan make:seed CategoriesTableSeeder php artisan make:factory CategoryFactory --model=App\Models\Category
Above commands will generate the database seed in database/seeds
folder and a factory class in database/factories
folder.
Learn More
You can learn more about how to generate dummy data in Laravel by reading our post Using Laravel Model Factories To Generate Dummy Data.
Open the CategoriesTableSeeder class and add the below content in this class.
use App\Models\Category; use Illuminate\Database\Seeder; class CategoriesTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Category::create([ 'name' => 'Root', 'description' => 'This is the root category, don\'t delete this one', 'parent_id' => null, 'menu' => 0, ]); factory('App\Models\Category', 10)->create(); } }
Now we will update our factory class. Open the CategoryFactory class and update with the below code.
use App\Models\Category; use Faker\Generator as Faker; $factory->define(Category::class, function (Faker $faker) { return [ 'name' => $faker->name, 'description' => $faker->realText(100), 'parent_id' => 1, 'menu' => 1, ]; });
Last thing, open the DatabaseSeeder class and add the blow in the run()
method.
$this->call(CategoriesTableSeeder::class);
After making all the changes, now we need to update our Category model class.
Adding Mutator for Category Model
Open the Category model class and add the below mutator for our name
field.
public function setNameAttribute($value) { $this->attributes['name'] = $value; $this->attributes['slug'] = str_slug($value); }
Above mutator will save the slug
field automatically whenever we save or create a category in our application.
Adding Parent Child Relationship for Categories
Next we will add couple of relationship defination in our Category model class.
The first relation will be to get the parent category of a category. For that, we will use the belongsTo to define the parent relationship.
public function parent() { return $this->belongsTo(Category::class, 'parent_id'); }
Now we will add the hasMany relationship for our category to return the children for our given category.
public function children() { return $this->hasMany(Category::class, 'parent_id'); }
Learn More
You can learn more about how to define the one to many relationship in Laravel by reading Introducing One To Many Relationship in Laravel post.
Conclusion
That’s it for now, in the next post we will start creating the categories admin area from where we will manage all our categories.
Code Repository
You can find the code base of this series on Laravel eCommerce Application repository.
If you have any question about this post, please leave a comment in the comment box below.