In this video we are going to learn about Shopping Cart Using Database
The shipping cart we were using till now was using Session.
Now lets add the database table for this shopping cart for storing the cat item for every user individually.
For that first of all lets create the migration for shopping cart
So switch to the command prompt and execute this command

php artisan vendor:publish --provider=\"Gloudemans\\Shoppingcart\\ShoppingcartServiceProvider\" --tag=\"migrations“

Now switch to project and lets see the migration
So go inside the database directory
Then open this shopping cart migration
And here you can see in this migration these columns are added
For storing the cart item for each user

public function up()
{
Schema::create(config('cart.database.table'), function (Blueprint $table) {
$table->string('identifier');
$table->string('instance');
$table->longText('content');
$table->nullableTimestamps();

$table->primary(['identifier', 'instance']);
});
}


Alright now lets migration the migration
So switch to the command prompt and type the command

php artisan migrate


Now lets store the cart item in the table
So lets open the ShopComponent class file and inside the render method
First of all lets check user is authenticated or not and then call the store method

if(Auth::check())
{
Cart::instance('cart')->store(Auth::user()->email);
}


Make sure you have imported Auth on top

use Illuminate\\Support\\Facades\\Auth;


Now open the CartComponent Class file
And here inside the render method Write same thing
First check the user authenticated or not then call the store method

if(Auth::check())
{
Cart::instance('cart')->store(Auth::user()->email);
}


Make sure you have imported Auth on top

use Illuminate\\Support\\Facades\\Auth;

Now lets restore the shopping cart after user login
So for that Go to the homecomponent class file
And here inside the render method
First check here user is authenticated or not then call the restore method

if(Auth::check())
{
Cart::instance('cart')->restore(Auth::user()->email);
}

Make sure you have imported Auth on top

use Illuminate\\Support\\Facades\\Auth;

Now all done lets check it
So switch to browser and refresh the page
And lets login with user credential
So enter the email id and password here
Now click on login
Now user is logged in
Now lets add some item to the cart
lets add 3 product
Now lets logout
And here you can see no item in cart now
Now lets login again
And here you can see the cart item of current user
If I login with different user credential
You can see cart is empty
Now lets add some product to the cart
This time lets add 2 product To the cart and then logout
If I login with first user credential
You can see here 3 item in cart
And if I login with 2nd user credential
Here you can see the 2 items in cart
So in this way you can Create Shopping Cart Using Database