Docs / Laravel Cart / Storage Drivers

Storage Drivers

The cart supports multiple storage backends via a driver system.

Session (default)

Cart data is stored in the Laravel session. No configuration needed.

Database

Store the cart in a database table for persistence across sessions. The package doesn't ship a migration or model for this - you create both yourself.

1. Create a migration:

$table->string('session_id');
$table->text('items');
$table->text('conditions');

2. Add casts to your model:

protected $guarded = [];

protected $casts = [
    'items'      => 'array',
    'conditions' => 'array',
];

3. Update config/cart.php:

'driver' => 'database',

'storage' => [
    'database' => [
        'model'      => \App\Models\Cart::class,
        'id'         => 'session_id',
        'items'      => 'items',
        'conditions' => 'conditions',
    ],
],

Redis

Store the cart in Redis with an optional TTL.

'driver' => 'redis',

'drivers' => [
    'redis' => [
        'connection' => 'default',
        'ttl'        => 604800, // 7 days in seconds
    ],
],

Multi-driver

Write to multiple drivers simultaneously. Reads come from the first driver listed.

'driver' => 'multi',

'drivers' => [
    'multi' => ['session', 'database'],
],

Custom driver

Generate a custom driver stub with php artisan cart:make:driver MyDriver, which implements CartDriver in app/Cart/Drivers. Register it with Cart::extend() from a service provider's boot() method:

use Wearepixel\Cart\Cart;
use App\Cart\Drivers\MyDriver;

Cart::extend('my-driver', fn ($sessionKey, $config) => new MyDriver($sessionKey));

Then set it as the active driver:

'driver' => 'my-driver',
Discovery Call

One call.
We'll both know.

20 minutes to walk through your project, ask the hard questions, and work out honestly if we're the right team for it.

  • One call per day - it gets our full attention
  • Australia's only Laravel Premier Partner
  • Senior engineers only - no juniors on your project
  • Brisbane-based, onshore team

Press Esc to close  ·  B to reopen