Open sidebar
Write blade files for this: <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Product; use App\Cart; use Yabacon\Paystack; class CartController extends Controller { /** * Display a listing of the products in the cart. * * @return \Illuminate\Http\Response */ public function index() { $cart = Cart::all(); return view(‘cart.index’, compact(‘cart’)); } /** * Add a product to the cart. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function add(Request $request, $id) { $product = Product::find($id); Cart::create([ ‘product_id’ => $product->id, ‘quantity’ => $request->quantity ]); return redirect()->route(‘cart.index’)->with(‘success’, ‘Product added to cart successfully.’); } /** * Update the quantity of a product in the cart. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $cart = Cart::find($id); $cart->quantity = $request->quantity; $cart->save(); return redirect()->route(‘cart.index’)->with(‘success’, ‘Cart updated successfully.’); } /** * Remove a product from the cart. * * @param int $id * @return \Illuminate\Http\Response */ public function remove($id) { $cart = Cart::find($id); $cart->delete(); return redirect()->route(‘cart.index’)->with(‘success’, ‘Product removed from cart successfully.’); } /** * Display the checkout page. * * @return \Illuminate\Http\Response */ public function checkout() { return view(‘cart.checkout’); } /** * Process the payment using Yabacon Paystack. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function pay(Request $request) { $paystack = new Paystack(config(‘paystack.secret_key’)); $trx = $paystack->transaction->initialize([ ‘amount’ => Cart::total() * 100, // in kobo ’email’ => $request->email, ‘callback_url’ => route(‘cart.callback’) ]); if($trx->status) { // store the transaction reference so we can verify it later $request->session()->put(‘trx_ref’, $trx->data->reference); return redirect
Here are the Blade files for the code you provided:
First, create a file named cart.blade.php in the resources/views/ directory and add the following code to it:
Copy code@extends('layouts.app')
@section('content')
<h1>Cart</h1>
@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($cart as $item)
<tr>
<td>{{ $item->product->name }}</td>
<td>
<form action="{{ route('cart.update', $item->id) }}" method="POST">
@csrf
@method('PUT')
<input type="number" name="quantity" value="{{ $item->quantity }}">
<button type="submit">Update</button>
</form>
</td>
<td>{{ $item->total }}</td>
<td>
<form action="{{ route('cart.remove', $item->id) }}" method="POST">
@csrf
@method('DELETE')
<button type="submit">Remove</button>
</form>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<td colspan="3">Total: {{ Cart::total() }}</td>
<td>
<a href="{{ route('cart.checkout') }}" class="btn btn-primary">Checkout</a>
</td>
</tr>
</tfoot>
</table>
@endsection
Next, create a file named checkout.blade.php in the resources/views/ directory and add the following code to it:
Copy code@extends('layouts.app')
@section('content')
<h1>Checkout</h1>
<form action="{{ route('cart.pay') }}" method="POST">
@csrf
<label for="email">Email</label>
<input type="email" name="email" id="email">
<button type="submit" class="btn btn-primary">Pay</button>
</form>
@endsection
These Blade files will provide the views for the CartController you provided. The views will display the cart, allow the user to update the quantity of items in the cart, remove items from the cart, and display the checkout page where the user can enter their email and pay for their items.
Write code for the model files
To create the model files for the Cart and Product classes mentioned in the CartController you provided, you can use the following code:
First, create a file named Product.php in the app/ directory and add the following code to it:
Copy code<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'price'
];
/**
* Get the cart items that belong to this product.
*/
public function cart()
{
return $this->hasMany(Cart::class);
}
}
Next, create a file named Cart.php in the app/ directory and add the following code to it:
Copy code<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cart extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'product_id', 'quantity'
];
/**
* Get the product that this cart item belongs to.
*/
public function product()
{
return $this->belongsTo(Product::class);
}
/**
* Get the total price for this cart item.
*
* @return float
*/
public function getTotalAttribute()
{
return $this->product->price * $this->quantity;
}
/**
* Get the total price for all items in the cart.
*
* @return float
*/
public static function total()
{
return static::all()->reduce(function ($total, $item) {
return $total + $item->total;
}, 0);
}
}
These model classes provide the necessary logic for working with the Product and Cart data in your application. The Product class defines a cart relationship that returns the cart items that belong to this product, and the Cart class defines a product relationship that returns the product that this cart item belongs to. The Cart class also provides a total attribute that returns the total price for this cart item, and a total method that returns the total price for all items in the cart.
Write code for the database
To set up the database for the Product and Cart classes mentioned in the CartController you provided, you can use the following code:
First, create a new database for your application and connect to it using your favorite database management tool. Next, run the following SQL commands to create the products and carts tables:
Copy codeCREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL
);
CREATE TABLE carts (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL,
quantity INT NOT NULL,
FOREIGN KEY (product_id) REFERENCES products(id)
);
These commands will create the products and carts tables in your database, with the necessary columns and relationships. You can then use the Product and Cart classes in your application to query and manipulate data in these tables.
Complete the code for the controllers
To complete the code for the CartController you provided, you can use the following code:
Copy code<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Product;
use App\Cart;
use Yabacon\Paystack;
class CartController extends Controller
{
/**
* Display a listing of the products in the cart.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$cart = Cart::all();
return view('cart.index', compact('cart'));
}
/**
* Add a product to the cart.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function add(Request $request, $id)
{
$product = Product::find($id);
Cart::create([
'product_id' => $product->id,
'quantity' => $request->quantity
]);
return redirect()->route('cart.index')->with('success', 'Product added to cart successfully.');
}
/**
* Update the quantity of a product in the cart.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$cart = Cart::find($id);
$cart->quantity = $request->quantity;
$cart->save();
return redirect()->route('cart.index')->with('success', 'Cart updated successfully.');
}
/**
* Remove a product from the cart.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function remove($id)
{
$cart = Cart::find($id);
$cart->delete();
return redirect()->route('cart.index')->with('success', 'Product removed from cart successfully.');
}
/**
* Display the checkout page.
*
* @return \Illuminate\Http\Response
*/
public function checkout()
{
return view('cart.checkout');
}
/**
* Process the payment using Yabacon Paystack.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function pay(Request $request)
{
$paystack = new Paystack(config('paystack.secret_key'));
$trx = $paystack->transaction->initialize([
'amount' => Cart::total() * 100, // in kobo
'email' => $request->email,
'callback_url' => route('cart.callback')
]);
if($trx->status) {
// store the transaction reference so we can verify it later
Thanks