Laravel Shopping Cart Tutorial| Add to Cart
This commit is contained in:
parent
f14a8026b5
commit
080094d2dd
@ -3,6 +3,7 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Models\Cart;
|
||||||
use App\Models\Products;
|
use App\Models\Products;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
@ -138,9 +139,19 @@ class MainController extends Controller
|
|||||||
return view('blogDetails');
|
return view('blogDetails');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function cart()
|
public function addToCart(Request $data)
|
||||||
{
|
{
|
||||||
return view('cart');
|
if (session()->has('id')) {
|
||||||
|
$item = new Cart();
|
||||||
|
$item->quantity = $data->input('quantity');
|
||||||
|
$item->productId = $data->input('id');
|
||||||
|
$item->customerId = session()->get('id');
|
||||||
|
|
||||||
|
$item->save();
|
||||||
|
return redirect()->back()->with('success', 'Item added to cart successfully!');
|
||||||
|
} else {
|
||||||
|
return redirect('/login')->with('error', 'Please login to add item to cart!');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function checkout()
|
public function checkout()
|
||||||
|
11
app/Models/Cart.php
Normal file
11
app/Models/Cart.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Cart extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
}
|
31
database/migrations/2024_07_04_080725_create_carts_table.php
Normal file
31
database/migrations/2024_07_04_080725_create_carts_table.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('carts', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->integer('productId')->default(0);
|
||||||
|
$table->integer('customerId')->default(0);
|
||||||
|
$table->integer('quantity')->default(0);
|
||||||
|
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('carts');
|
||||||
|
}
|
||||||
|
};
|
@ -61,12 +61,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="tab-pane" id="tabs-3" role="tabpanel">
|
<div class="tab-pane" id="tabs-3" role="tabpanel">
|
||||||
<div class="product__details__pic__item">
|
<div class="product__details__pic__item">
|
||||||
<img src="img/shop-details/product-big.png" alt="">
|
<img src="{{ URL::asset('uploads/products/' . $products->picture) }}" alt="">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="tab-pane" id="tabs-4" role="tabpanel">
|
<div class="tab-pane" id="tabs-4" role="tabpanel">
|
||||||
<div class="product__details__pic__item">
|
<div class="product__details__pic__item">
|
||||||
<img src="img/shop-details/product-big-4.png" alt="">
|
<img src="{{ URL::asset('uploads/products/' . $products->picture) }}" alt="">
|
||||||
<a href="https://www.youtube.com/watch?v=8PJ3_p7VqHw&list=RD8PJ3_p7VqHw&start_radio=1"
|
<a href="https://www.youtube.com/watch?v=8PJ3_p7VqHw&list=RD8PJ3_p7VqHw&start_radio=1"
|
||||||
class="video-popup"><i class="fa fa-play"></i></a>
|
class="video-popup"><i class="fa fa-play"></i></a>
|
||||||
</div>
|
</div>
|
||||||
@ -76,6 +76,9 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<form action="{{ url::to('addToCart') }}" method="post">
|
||||||
|
@csrf
|
||||||
|
|
||||||
<div class="product__details__content">
|
<div class="product__details__content">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row d-flex justify-content-center">
|
<div class="row d-flex justify-content-center">
|
||||||
@ -129,19 +132,22 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="product__details__cart__option">
|
<div class="product__details__cart__option">
|
||||||
<div class="quantity">
|
<div class="quantity">
|
||||||
<div class="pro-qty">
|
|
||||||
<input type="text" value="1">
|
<input type="number" name="quantity" class="form-control" min="1"
|
||||||
|
max="{{ $products->quantity }}" value="1">
|
||||||
|
<input type="hidden" name="id" value="{{ $products->id }}">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
<button type="submit" name="addToCart" class="primary-btn">Add to Cart</button>
|
||||||
<a href="#" class="primary-btn">add to cart</a>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="product__details__btns__option">
|
<div class="product__details__btns__option">
|
||||||
<a href="#"><i class="fa fa-heart"></i> add to wishlist</a>
|
<a href="#"><i class="fa fa-heart"></i> add to wishlist
|
||||||
|
</a>
|
||||||
<a href="#"><i class="fa fa-exchange"></i> Add To Compare</a>
|
<a href="#"><i class="fa fa-exchange"></i> Add To Compare</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="product__details__last__option">
|
<div class="product__details__last__option">
|
||||||
<h5><span>Guaranteed Safe Checkout</span></h5>
|
<h5><span>Guaranteed Safe Checkout</span></h5>
|
||||||
<img src="img/shop-details/details-payment.png" alt="">
|
<img src="{{ url::asset('img/shop-details/details-payment.png') }}" alt="">
|
||||||
<ul>
|
<ul>
|
||||||
<li><span>SKU:</span> 3812912</li>
|
<li><span>SKU:</span> 3812912</li>
|
||||||
<li><span>Categories:</span> Clothes</li>
|
<li><span>Categories:</span> Clothes</li>
|
||||||
@ -171,7 +177,8 @@
|
|||||||
<div class="tab-content">
|
<div class="tab-content">
|
||||||
<div class="tab-pane active" id="tabs-5" role="tabpanel">
|
<div class="tab-pane active" id="tabs-5" role="tabpanel">
|
||||||
<div class="product__details__tab__content">
|
<div class="product__details__tab__content">
|
||||||
<p class="note">Nam tempus turpis at metus scelerisque placerat nulla deumantos
|
<p class="note">Nam tempus turpis at metus scelerisque placerat nulla
|
||||||
|
deumantos
|
||||||
solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis
|
solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis
|
||||||
ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo
|
ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo
|
||||||
pharetras loremos.</p>
|
pharetras loremos.</p>
|
||||||
@ -235,7 +242,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="tab-pane" id="tabs-7" role="tabpanel">
|
<div class="tab-pane" id="tabs-7" role="tabpanel">
|
||||||
<div class="product__details__tab__content">
|
<div class="product__details__tab__content">
|
||||||
<p class="note">Nam tempus turpis at metus scelerisque placerat nulla deumantos
|
<p class="note">Nam tempus turpis at metus scelerisque placerat nulla
|
||||||
|
deumantos
|
||||||
solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis
|
solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis
|
||||||
ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo
|
ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo
|
||||||
pharetras loremos.</p>
|
pharetras loremos.</p>
|
||||||
@ -273,6 +281,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</form>
|
||||||
</section>
|
</section>
|
||||||
<!-- Shop Details Section End -->
|
<!-- Shop Details Section End -->
|
||||||
|
|
||||||
@ -287,7 +296,8 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-3 col-md-6 col-sm-6 col-sm-6">
|
<div class="col-lg-3 col-md-6 col-sm-6 col-sm-6">
|
||||||
<div class="product__item">
|
<div class="product__item">
|
||||||
<div class="product__item__pic set-bg" data-setbg="{{ url::asset('img/product/product-1.jpg') }}">
|
<div class="product__item__pic set-bg"
|
||||||
|
data-setbg="{{ url::asset('img/product/product-1.jpg') }}">
|
||||||
<span class="label">New</span>
|
<span class="label">New</span>
|
||||||
<ul class="product__hover">
|
<ul class="product__hover">
|
||||||
<li><a href="#"><img src="{{ url::asset('img/icon/heart.png') }}"
|
<li><a href="#"><img src="{{ url::asset('img/icon/heart.png') }}"
|
||||||
@ -326,7 +336,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="col-lg-3 col-md-6 col-sm-6 col-sm-6">
|
<div class="col-lg-3 col-md-6 col-sm-6 col-sm-6">
|
||||||
<div class="product__item">
|
<div class="product__item">
|
||||||
<div class="product__item__pic set-bg" data-setbg="{{url::asset('img/product/product-2.jpg')}}">
|
<div class="product__item__pic set-bg"
|
||||||
|
data-setbg="{{ url::asset('img/product/product-2.jpg') }}">
|
||||||
<ul class="product__hover">
|
<ul class="product__hover">
|
||||||
<li><a href="#"><img src="{{ url::asset('img/icon/heart.png') }}"
|
<li><a href="#"><img src="{{ url::asset('img/icon/heart.png') }}"
|
||||||
alt=""></a></li>
|
alt=""></a></li>
|
||||||
@ -364,7 +375,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="col-lg-3 col-md-6 col-sm-6 col-sm-6">
|
<div class="col-lg-3 col-md-6 col-sm-6 col-sm-6">
|
||||||
<div class="product__item sale">
|
<div class="product__item sale">
|
||||||
<div class="product__item__pic set-bg" data-setbg="{{url::asset('img/product/product-3.jpg')}}">
|
<div class="product__item__pic set-bg"
|
||||||
|
data-setbg="{{ url::asset('img/product/product-3.jpg') }}">
|
||||||
<span class="label">Sale</span>
|
<span class="label">Sale</span>
|
||||||
<ul class="product__hover">
|
<ul class="product__hover">
|
||||||
<li><a href="#"><img src="{{ url::asset('img/icon/heart.png') }}"
|
<li><a href="#"><img src="{{ url::asset('img/icon/heart.png') }}"
|
||||||
|
@ -8,7 +8,9 @@ Route::get('/', [MainController::class, 'index']);
|
|||||||
Route::get('/about', [MainController::class, 'about']);
|
Route::get('/about', [MainController::class, 'about']);
|
||||||
Route::get('/blog', [MainController::class, 'blog']);
|
Route::get('/blog', [MainController::class, 'blog']);
|
||||||
Route::get('/blogDetails', [MainController::class, 'blogDetails']);
|
Route::get('/blogDetails', [MainController::class, 'blogDetails']);
|
||||||
Route::get('/cart', [MainController::class, 'cart']);
|
|
||||||
|
Route::post('/addToCart', [MainController::class, 'addToCart']);
|
||||||
|
|
||||||
Route::get('/checkout', [MainController::class, 'checkout']);
|
Route::get('/checkout', [MainController::class, 'checkout']);
|
||||||
Route::get('/main', [MainController::class, 'main']);
|
Route::get('/main', [MainController::class, 'main']);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user