Docs / Laravel Cart / Storage Options

Storage Options

By default the cart is stored in the session, but there are times you may want to store the cart in the database.

For instance, you may want to store the cart in the database so that the cart can be retrieved even after the user logs out or closes the browser, or you may want to add cart timeouts and support for multiple computers.

Session

The cart is stored in the session by default, using Laravel's in-built SessionManager.

Database Support

To get started, you'll need to create a new model for your Cart, the package requires only 3 columns, but you're free to extend this as you wish and add more columns.

You can utilise the events provided by the package to store additional information alongside the cart.

$table->string('session_id'); // this handles the session id of the cart $table->text('items'); // this will store the cart items $table->text('conditions'); // this will store the cart level conditions

Then add some json casts to your model and fillable columns:

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

Then update the configuration file to use the database driver:

'driver' => 'database', 'storage' => [ 'session', 'database' => [ 'model' => \App\Models\Cart::class, // your model here 'id' => 'session_id', 'items' => 'items', 'conditions' => 'conditions', ], ],
Discovery Call

One call.
We'll both know.

An hour 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