Creating and migrating the carts database

As you know, all e-commerce applications should have a cart. In this application, we’ll have a cart too. We’ll design a member-based cart, which means we can store and show each visitor their carts and cart items. So, we need a Cart model to store the cart items. It will be a very simple structure. Let’s create the Cart.php file under app/models and add following code:

Class Cart extends Eloquent {

protected $table = 'carts';

protected $fillable = array('member_id','book_id','amount','total');

public function Books(){

return $this->belongsTo('Book','book_id');



Now we need a migration file to create the carts table. To create a migration file, give a command such as the following:

php artisan make:mgration  create_carts_table --table=carts --create

Open the migration file, which was created recently and located at database/migrations/. We need to edit the up() function as shown in the following code snippet:

  public function up()
    Schema::create('carts', function(Blueprint $table)
      $table->decimal('total', 10, 2);

To apply migration, we need to migrate with the following artisan command:

php artisan migrate
Chapter 2 of 6