Laravel E-Commerce Application Development – Attributes 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 part 10 of the Laravel E-Commerce Application Development series. In this part will add the model, migrations, and seed for attributes 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.
The basic concept of using attributes for our products (which we will cover in the coming posts) will be to attach extra information with our product. For example, a product can have a different size and color. Every color or size can have some extra price, which will be added to the product’s price.
Keeping that in mind, we will need to create two different models, Attribute and AttributeValue. For example, color or size can be stored in attributes
table and their values like red, black, blue, small, medium and large can be stored in the attribute_values
table.
So let’s start implementing this in our application.
Creating Attributes Model and Migration
To save our attributes in database table we will need to create a migration and model. Open your command terminal and run the below command to generate a model and migration for the attribute.
php artisan make:model Models\Attribute -m
The -m
flag will create a migration file for our Attribute model. Above command will generate a Attribute model in app/Models
folder and a migration file for this model in database/migrations
folder.
Open your migration file and update with the below class.
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateAttributesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('attributes', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('code')->unique(); $table->string('name'); $table->enum('frontend_type', ['select', 'radio', 'text', 'text_area']); $table->boolean('is_filterable')->default(0); $table->boolean('is_required')->default(0); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('attributes'); } }
In this migration file, we are adding the code column whose value will be unique. The name column which will be displayed on the frontend of the website. An enum column for frontend type which can be a select box, radio button, input box or a text area. Next, we add is_filterable and is_required columns with boolean data type.
Now, open you Attribute model from app/Models
folder and update the whole class with the below one.
namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * Class Attribute * @package App\Models */ class Attribute extends Model { /** * @var string */ protected $table = 'attributes'; /** * @var array */ protected $fillable = [ 'code', 'name', 'frontend_type', 'is_filterable', 'is_required' ]; /** * @var array */ protected $casts = [ 'is_filterable' => 'boolean', 'is_required' => 'boolean', ]; }
In the above model class, we have defined the table for our attributes and fillable property for mass assignment. We also added the $casts
property to return our is_filterable
and is_required
values in boolean format.
Creating Attributes Seed Class
Next, we will add a seeder class for Attribute model. Run the below command to generate a seed class.
php artisan make:seed AttributesTableSeeder
Now open the AttributesTableSeeder class from database/seeds
folder and update with the below one.
use App\Models\Attribute; use Illuminate\Database\Seeder; class AttributesTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // Create a size attribute Attribute::create([ 'code' => 'size', 'name' => 'Size', 'frontend_type' => 'select', 'is_filterable' => 1, 'is_required' => 1, ]); // Create a color attribute Attribute::create([ 'code' => 'color', 'name' => 'Color', 'frontend_type' => 'select', 'is_filterable' => 1, 'is_required' => 1, ]); } }
Now, open the DatabaseSeeder class from seeds folder and update with the below one.
use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { // $this->call(UsersTableSeeder::class); $this->call(AdminsTableSeeder::class); $this->call(SettingsTableSeeder::class); $this->call(CategoriesTableSeeder::class); $this->call(AttributesTableSeeder::class); } }
Now open your command terminal and run the below commands to migrate the attributes table and add the color and size attributes in it.
php artisan migrate php artisan db:seed --class=AttributesTableSeeder
If you check your database, you will find a new table named attributes with two rown inserted for size
and color
.
Creating Attribute Values Model and Migration
In this section, we will create AttributeValue model which will host the values for our attributes. Again open your terminal and run below command.
php artisan make:model Models\AttributeValue -m
Above command will create a migration for attribute_values
table in database/migrations
folder. Open this migration file and update with the below one.
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateAttributeValuesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('attribute_values', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('attribute_id'); $table->foreign('attribute_id')->references('id')->on('attributes'); $table->text('value'); $table->decimal('price', 2)->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('attribute_values'); } }
Using above migration, we are adding a attribute_id
column which has a foreign key on attributes
table. Next, a value column with text as it’s datatype. You can also use the string. Then a price column which is nullable.
Open the AttributeValue model and replace with the below one.
namespace App\Models; use Illuminate\Database\Eloquent\Model; /** * Class AttributeValue * @package App\Models */ class AttributeValue extends Model { /** * @var string */ protected $table = 'attribute_values'; /** * @var array */ protected $fillable = [ 'attribute_id', 'value', 'price' ]; /** * @var array */ protected $casts = [ 'attribute_id' => 'integer', ]; }
Creating Attribute Values Seed Class
In this section, we will create a seed class for our AttributeValue model. Run the below command in terminal to generate a seed class.
php artisan make:seed AttributeValuesTableSeeder
Open this seed class and update it with the below one.
use App\Models\AttributeValue; use Illuminate\Database\Seeder; class AttributeValuesTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { $sizes = ['small', 'medium', 'large']; $colors = ['black', 'blue', 'red', 'orange']; foreach ($sizes as $size) { AttributeValue::create([ 'attribute_id' => 1, 'value' => $size, 'price' => null, ]); } foreach ($colors as $color) { AttributeValue::create([ 'attribute_id' => 2, 'value' => $color, 'price' => null, ]); } } }
In this seed class, we are simply adding some sizes and colors in the attribute_values
table.
Now, run the below commands.
php artisan migrate php artisan db:seed --class=AttributeValuesTableSeeder
Adding Relationship to Attributes and Attribute Values
In this section, we will add the relationship between our Attribute and AttributeValue models. Open the Attribute model and add the below function in it.
/** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function values() { return $this->hasMany(AttributeValue::class); }
In above code, we are defining a One To Many Relationship using hasMany()
method as our attribute will have many values.
Now, in our AttributeValue we will define the inverse of this relationship using the belongsTo
method as a value belongs to a attribute.
Open your AttributeValue model and add the below function.
/** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function attribute() { return $this->belongsTo(Attribute::class); }
Conclusion
That’s it for now, in the next post we will start adding a attributes section in our admin area.
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.