Laravel Shopping Cart Tutorial| Add to Cart
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Cart;
|
||||
use App\Models\Products;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
@ -138,9 +139,19 @@ class MainController extends Controller
|
||||
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()
|
||||
|
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;
|
||||
}
|
Reference in New Issue
Block a user