Overview
The Cart model represents shopping cart functionality in the restaurant management system. It manages temporary storage of food items that users intend to order.
Model Definition
namespace App\Models ;
use Illuminate\Database\Eloquent\Factories\ HasFactory ;
use Illuminate\Database\Eloquent\ Model ;
class Cart extends Model
{
use HasFactory ;
}
The current Cart model implementation does not define explicit fillable fields. Consider adding the $fillable property to allow mass assignment for cart operations.
Database Fields
Based on the database migration, the cart table includes the following fields:
Reference to the user who owns this cart item
Database Type: string
Nullable: Yes
Example: “1”, “42”
Reference to the food item in the cart
Database Type: string
Nullable: Yes
Example: “5”, “18”
Quantity of the food item
Database Type: string
Nullable: Yes
Example: “2”, “5”
Database Schema
Schema :: create ( 'carts' , function ( Blueprint $table ) {
$table -> id ();
$table -> string ( 'user_id' ) -> nullable ();
$table -> string ( 'food_id' ) -> nullable ();
$table -> string ( 'quantity' ) -> nullable ();
$table -> timestamps ();
});
Relationships
Show Potential Relationships (Not Currently Defined)
The Cart model would benefit from defining the following relationships: public function user ()
{
return $this -> belongsTo ( User :: class );
}
Links the cart item to the user who added it. public function food ()
{
return $this -> belongsTo ( Food :: class );
}
Links the cart item to the food item being ordered.
Recommended Implementation
Here’s a recommended enhanced implementation of the Cart model:
namespace App\Models ;
use Illuminate\Database\Eloquent\Factories\ HasFactory ;
use Illuminate\Database\Eloquent\ Model ;
class Cart extends Model
{
use HasFactory ;
protected $fillable = [
'user_id' ,
'food_id' ,
'quantity' ,
];
/**
* Get the user that owns the cart item.
*/
public function user ()
{
return $this -> belongsTo ( User :: class );
}
/**
* Get the food item in the cart.
*/
public function food ()
{
return $this -> belongsTo ( Food :: class );
}
/**
* Get the total price for this cart item.
*/
public function getTotalPrice ()
{
return $this -> food -> price * $this -> quantity ;
}
}
Usage Examples
Adding Items to Cart
// Add item to cart
Cart :: create ([
'user_id' => auth () -> id (),
'food_id' => 5 ,
'quantity' => 2
]);
// Or using mass assignment (if fillable is set)
$cart = new Cart ();
$cart -> user_id = auth () -> id ();
$cart -> food_id = 5 ;
$cart -> quantity = 2 ;
$cart -> save ();
Retrieving Cart Items
// Get all cart items for current user
$cartItems = Cart :: where ( 'user_id' , auth () -> id ()) -> get ();
// Get cart items with food details (if relationships defined)
$cartItems = Cart :: where ( 'user_id' , auth () -> id ())
-> with ( 'food' )
-> get ();
// Calculate total cart value
$total = Cart :: where ( 'user_id' , auth () -> id ())
-> with ( 'food' )
-> get ()
-> sum ( function ( $item ) {
return $item -> food -> price * $item -> quantity ;
});
Updating Cart Items
// Update quantity
$cartItem = Cart :: where ( 'user_id' , auth () -> id ())
-> where ( 'food_id' , 5 )
-> first ();
if ( $cartItem ) {
$cartItem -> quantity = 3 ;
$cartItem -> save ();
}
// Increment quantity
$cartItem -> increment ( 'quantity' );
Removing Cart Items
// Remove specific item
Cart :: where ( 'user_id' , auth () -> id ())
-> where ( 'food_id' , 5 )
-> delete ();
// Clear entire cart for user
Cart :: where ( 'user_id' , auth () -> id ()) -> delete ();
Converting Cart to Order
// Get cart items and create order
$cartItems = Cart :: where ( 'user_id' , auth () -> id ())
-> with ( 'food' )
-> get ();
foreach ( $cartItems as $item ) {
Order :: create ([
'foodname' => $item -> food -> title ,
'quantity' => $item -> quantity ,
'price' => $item -> food -> price * $item -> quantity ,
'name' => auth () -> user () -> name ,
'phone' => auth () -> user () -> phone ,
'address' => auth () -> user () -> address ,
]);
}
// Clear cart after order is placed
Cart :: where ( 'user_id' , auth () -> id ()) -> delete ();
Timestamps
created_at: Automatically set when the cart item is added
updated_at: Automatically updated when the cart item is modified
Recommendations
Data Type Issues: The current schema uses string types for user_id, food_id, and quantity. Consider migrating these to appropriate types:
user_id → unsignedBigInteger with foreign key constraint
food_id → unsignedBigInteger with foreign key constraint
quantity → integer or unsignedInteger
Recommended Enhancements:
Add $fillable property to enable mass assignment
Define user() and food() relationship methods
Add validation rules for cart operations
Implement a method to calculate total price per cart item
Add foreign key constraints in the database migration
Consider adding a session_id field for guest cart functionality
Example Migration Enhancement
Schema :: create ( 'carts' , function ( Blueprint $table ) {
$table -> id ();
$table -> foreignId ( 'user_id' )
-> nullable ()
-> constrained ()
-> onDelete ( 'cascade' );
$table -> foreignId ( 'food_id' )
-> nullable ()
-> constrained ( 'food' )
-> onDelete ( 'cascade' );
$table -> unsignedInteger ( 'quantity' ) -> default ( 1 );
$table -> string ( 'session_id' ) -> nullable (); // For guest carts
$table -> timestamps ();
// Ensure no duplicate items in cart
$table -> unique ([ 'user_id' , 'food_id' ]);
});