Русский | English
Cart | Laravel |
---|---|
2.x | 8.x |
1.x | 7.x |
$ composer require webdevrus/laravel-cart
- Migrations
$ php artisan vendor:publish --provider="WebDevRus\LaravelCart\Providers\CartServiceProvider" --tag="migrations"
$ php artisan migrate
- Configuration
$ php artisan vendor:publish --provider="WebDevRus\LaravelCart\Providers\CartServiceProvider" --tag="config"
- Specify trait
HasCart
in modelUser
.
<?php
namespace App\Models;
use WebDevRus\LaravelCart\Traits\HasCart;
class User extends Authenticatable
{
use HasCart;
...
}
use WebDevRus\LaravelCart\Facades\Cart;
...
// Add item
Cart::add([
'product_id' => 1,
'quantity' => 3,
'price' => 500,
]);
// Remove item
Cart::remove([
'product_id' => 10,
]);
// Get item by index
Cart::item(3);
// Get cart
Cart::get();
// Clear cart
Cart::clear();
At the output Cart::get()
we get the contents of such a structure
[
"items" => [
0 => [
"price" => 500
"quantity" => 3
"product_id" => 1
]
...
]
"total_count" => 1
"total_price" => 1500
"total_quantity" => 3
]
If in
config/cart.php
keymerge
settrue
, then after user authorization, cart fromsession
will merged with the cartdatabase
.
For example, create a helper-class in the App\Helpers
namespace and name it Cart
.
He must inherit the main helper-class —
Helper
.
<?php
namespace App\Helpers;
use WebDevRus\LaravelCart\Helpers\Helper;
class Cart extends Helper
{
...
}
Don't forget to indicate this in the config/cart.php
'session' => [
...
'helper' => \App\Helpers\Cart::class,
],
'database' => [
...
'helper' => \App\Helpers\Cart::class,
],
Let's say we need to add a new type of output: instead of the available ones array
and object
— JSON
.
Go to config/cart.php
and change the key output
/**
* Specify the value json
*/
'output' => 'json',
Let's create a method toJson()
in App\Helpers\Cart
public function toJson(array $array): string
{
return json_encode($array);
}
And now Cart::get()
will return a string on JSON.
In the helper-class you can create two methods — before()
and after()
.
before()
— called before the cart is formed .
after()
— called after the cart has been formed and formatted.
Let's say we need to add a product model to the items in the cart. The after ()
method is suitable for us, because after before ()
the model object can be transformed.
In this case, the code will look something like this:
/**
* @param array|object $cart
* @return void
*/
public function after(&$cart)
{
$items = data_get($cart, 'items');
$model = \App\Models\Product::whereIn('id', Arr::pluck($items, 'product_id'))->get();
foreach ($items as $i => $item) {
$product = $model->firstWhere('id', data_get($item, 'product_id'));
data_set($cart, "items.{$i}.product", $product);
}
}
At the output we get the cart type array
or object
with a product model for each position.
$ composer test