mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
feat: database support
This commit is contained in:
+16
-5
@@ -6,6 +6,9 @@ class CartSession
|
||||
{
|
||||
private $customStorage;
|
||||
|
||||
public $sessionKey;
|
||||
public $driver;
|
||||
|
||||
private $session;
|
||||
|
||||
private $sessionId = null;
|
||||
@@ -14,12 +17,15 @@ class CartSession
|
||||
|
||||
public function __construct($session, $sessionKey, $config)
|
||||
{
|
||||
if ($config['driver'] === 'session') {
|
||||
$this->sessionKey = $sessionKey;
|
||||
$this->driver = $config['driver'];
|
||||
|
||||
if ($this->driver === 'session') {
|
||||
$this->customStorage = false;
|
||||
$this->session = $session;
|
||||
$this->itemsKey = $sessionKey . '_cart_items';
|
||||
$this->conditionsKey = $sessionKey . '_cart_conditions';
|
||||
} elseif ($config['driver'] === 'database') {
|
||||
$this->itemsKey = $this->sessionKey . '_cart_items';
|
||||
$this->conditionsKey = $this->sessionKey . '_cart_conditions';
|
||||
} elseif ($this->driver === 'database') {
|
||||
$this->customStorage = true;
|
||||
|
||||
$this->sessionId = $config['storage']['database']['id'];
|
||||
@@ -27,7 +33,7 @@ public function __construct($session, $sessionKey, $config)
|
||||
$this->conditionsKey = $config['storage']['database']['conditions'];
|
||||
|
||||
// find or create the session in the database
|
||||
$this->session = $session->firstOrNew([$this->sessionId => $sessionKey]);
|
||||
$this->session = $session->firstOrNew([$this->sessionId => $this->sessionKey]);
|
||||
|
||||
// initialize the items and conditions
|
||||
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
|
||||
@@ -99,4 +105,9 @@ public function clear()
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getSessionModel()
|
||||
{
|
||||
return $this->session;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user