Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
How to use Shopping Cart in Laravel?
https://github.com/darryldecode/laravelshoppingcart
Add Item
You can pass a simple or a multidimensional array and to help you get started, we have listed below all the default indexes that you can pass when adding or updating a cart item.
File name : index.php
Add a single item
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
]);
Add a single item with a custom index
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'sku' => 'tshirt-custom',
]);
Add a single item with attributes and a custom index
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'sku' => 'tshirt-red-large',
'attributes' => [
'color' => [
'label' => 'Red',
'value' => 'red',
],
'size' => [
'label' => 'Large',
'value' => 'l',
],
],
]);
Adding multiple items
Cart::add([
[
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'sku' => 'tshirt-red-large',
'attributes' => [
'color' => [
'label' => 'Red',
'value' => 'red',
],
'size' => [
'label' => 'Large',
'value' => 'l',
],
],
],
[
'id' => 'sweatshirt',
'name' => 'Sweatshirt',
'quantity' => 1,
'price' => 98.32,
],
]);
Update Item
Cart::update()
Update an item quantity
Cart::update('c14c437bc9ae7d35a7c18ee151c6acc0', ['quantity' => 2]);
Update a single item
Cart::update('c14c437bc9ae7d35a7c18ee151c6acc0', [
'quantity' => 1,
'price' => 12.50,
]);
Update multiple items
Cart::update([
'c14c437bc9ae7d35a7c18ee151c6acc0' => [
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
],
'63e2d7033fe95b9134a5737503d10ba5' => [
'id' => 'sweatshirt',
'name' => 'Sweatshirt',
'quantity' => 2,
'price' => 98.32,
],
]);
Remove Item
Remove a single item
Cart::remove('c14c437bc9ae7d35a7c18ee151c6acc0');
Remove multiple items
Cart::remove([
'c14c437bc9ae7d35a7c18ee151c6acc0',
'63e2d7033fe95b9134a5737503d10ba5',
]);
Get all the items
$items = Cart::items();
foreach ($items as $item)
{
echo $item->price();
}
Check if an item exists
if (Cart::exists('c14c437bc9ae7d35a7c18ee151c6acc0'))
{
Cart::remove('c14c437bc9ae7d35a7c18ee151c6acc0');
}
Get a single item
$item = Cart::item('c14c437bc9ae7d35a7c18ee151c6acc0');
Get the item price
$item->price();
Get the item price + the item attributes total price
$item->price(true);
Get the item quantity
$item->quantity();
Get the item subtotal
$item->subtotal();
Get the item weight
$item->weight();
Get the item attributes
$item->attributes();
Cart::total()
echo Cart::total();
Cart::subtotal()
echo Cart::subtotal();
Cart::quantity()
echo Cart::quantity();
Cart::weight()
echo Cart::weight();
Cart::itemsSubtotal()
echo Cart::itemsSubtotal();
Cart::clear()
Cart::clear();
Cart::getIdentity()
Cart::getIdentity();
Cart::setIdentity()
Cart::setIdentity('my-new-cart-name');
Cart::sync()
This method is very useful when you want to synchronize a shopping cart that is stored on the database for example.
$items = [
[
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
],
[
'id' => 'sweatshirt',
'name' => 'Sweatshirt',
'quantity' => 1,
'price' => 98.32,
],
];
$collection = new Collection($items);
Cart::sync($collection);
Metadata
Managing metadata inside the cart like shipping or billing information is very easy.
Setting metadata is very easy, just provide an array with a key/value pair and you're done.
Cart::setMetaData() :-
$data = [
'full_name' => 'John Doe',
'address' => 'Example Street',
];
Cart::setMetaData('shipping_information', $data);
Cart::setMetaData('shipping_information.street', 'Street ABC');
Cart::getMetaData()
To return all the available metadata
$metadata = Cart::getMetaData();
To return metadata by keys
$metadata = Cart::getMetaData('shipping_information');
$metadata = Cart::getMetaData('shipping_information.full_name');
Cart::removeMetaData()
To remove all the metadata
Cart::removeMetaData();
To remove metadata by keys
Cart::removeMetaData('shipping_information.full_name');
Cart::removeMetaData('shipping_information');
Search
You can use one or multiple properties to search for items in the cart.
Find
Cart::find([
'id' => 'foobar',
]);
Cart::find([
'name' => 'Foo Bar',
'price' => 5,
]);
Cart::find([
'attributes' => [
'size' => [
'price' => 5,
],
],
]);
Attributes
Each item can have different attributes like size, color and you can even add a price to each attribute that will reflect on the final item price.
Cart::add([
'id' => 'tshirt',
'name' => 'T-Shirt',
'quantity' => 1,
'price' => 12.50,
'attributes' => [
'size' => [
'label' => 'Large',
'value' => 'l',
'price' => 5,
],
'color' => [
'label' => 'Red',
'value' => 'red',
],
],
]);
count()
The count() method counts the number of items in the collection.
$total = Cart::items()->count();
isEmpty()
The isEmpty() method can be used to check whether or not the collection has elements within it. It accepts no value and returns a boolean.
if ( ! Cart::items()->isEmpty())
{
echo 'We have items on our cart :)';
}
else
{
echo 'Cart is empty :(';
}