Develop an E-Commerce Website With Laravel 5.4 - Part 8
All posts in this series:
- Develop an E-Commerce Website With Laravel 5.4 - Part 1
- Develop an E-Commerce Website With Laravel 5.4 - Part 2
- Develop an E-Commerce Website With Laravel 5.4 - Part 3
- Develop an E-Commerce Website With Laravel 5.4 - Part 4
- Develop an E-Commerce Website With Laravel 5.4 - Part 5
- Develop an E-Commerce Website With Laravel 5.4 - Part 6
- Develop an E-Commerce Website With Laravel 5.4 - Part 7
- Develop an E-Commerce Website With Laravel 5.4 - Part 8
We are on stage of adding items into the cart, for that we need to create a Controller, Model and a View page along with routes. I will not go thru the same commands again and again. Instead of it I am going to discuss how the cart logic was done.
I created the migration file which have the following schema:
public function up() {
Schema::create( 'carts', function ( Blueprint $table ) {
$table->increments( 'id' );
$table->string( 'product_name', 200 );
$table->string( 'session_id' )->default( '0' );
$table->integer( 'product_id' )->default( 0 );
$table->float( 'price' )->default( 0 );
$table->integer( 'qty' )->default( 0 );
$table->timestamps();
} );
}
I am trying to keep simple. Cart items are being stored in db and in order to have a unique cart per session I am using _token
session variable offered by Laravel.
I also added three new routes:
Route::get( 'cart/add/{product_id}', 'CartController@add' )->name('cart.add');
Route::get( 'cart/', 'CartController@index' );
Route::get( 'cart/show', 'CartController@show' )->name('cart.show');
So now in add
method of the CartController
looks like this:
public function add( $product_id ) {
// For Identification Purpose
$session_id = session()->get( '_token' );
// Get Product Detils by ID
$product = Product::where( 'id', $product_id )->first();
if ( $product == null ) {
return abort( 404 );
}
if ( Cart::where( 'session_id', '=', $session_id )->exists() ) {
//CHeck whether product exist if yes increase quantity
$entry = Cart::where( [ 'session_id' => $session_id, 'product_id' => $product_id ] )->increment( 'qty', 1 );
if ( ! $entry ) {
Cart::create( [
'session_id' => $session_id,
'product_id' => $product_id,
'product_name' => $product['title'],
'price' => $product['discount_price'],
'qty' => 1
] );
}
} else {
Cart::create( [
'session_id' => $session_id,
'product_id' => $product_id,
'product_name' => $product['title'],
'price' => $product['discount_price'],
'qty' => 1
] );
}
// First check whether the cart exist
return redirect()->route( 'cart.show' );
}
I am passing product_id
in route and thus being checked for adding/updating cart items.
That's it my friend. The new post will be the last post in the series in which we will create order out of Cart Items and make payment via Stripe. Stay tuned!
Liked my articles, want me to write more on different topics? You can send me token of appreciation by donating amount on below Crypto coins addresses. Thanks
BTC = 12stJs8vZNuuVfjZSSzpLPA96quNissk1b
ETH = 0x23f016d7a8e408e5551ae7aa51b3fe1534165463
AOA sir i face the error at (if ( Cart::where( ‘session_id’, ‘=’, $session_id )->exists() )
session is not found . i register my self and get id ($session_id =Auth::id(); (that get regiser id =1). please help me and also explain. thanks in advance