1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ibrand/laravel-shopping-cart. |
5
|
|
|
* |
6
|
|
|
* (c) iBrand <https://www.ibrand.cc> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace iBrand\Shoppingcart\Storage; |
13
|
|
|
|
14
|
|
|
use DB; |
15
|
|
|
use iBrand\Shoppingcart\Item; |
16
|
|
|
use Illuminate\Database\Eloquent\Collection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class DatabaseStorage. |
20
|
|
|
*/ |
21
|
|
|
class DatabaseStorage implements Storage |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $table = 'shopping_cart'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $filed = ['__raw_id', 'id', 'name', 'qty', 'price', 'total', '__model', 'type', 'status']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $key |
35
|
|
|
* @param $values |
36
|
|
|
*/ |
37
|
|
|
public function set($key, $values) |
38
|
|
|
{ |
39
|
|
|
if (is_null($values)) { |
40
|
|
|
$this->forget($key); |
41
|
|
|
|
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$rawIds = $values->pluck('__raw_id')->toArray(); |
46
|
|
|
|
47
|
|
|
//Delete the data that has been removed from cart. |
48
|
|
|
DB::table($this->table)->whereNotIn('__raw_id', $rawIds)->where('key', $key)->delete(); |
49
|
|
|
|
50
|
|
|
$keys = explode('.', $key); |
51
|
|
|
|
52
|
|
|
$userId = end($keys); |
53
|
|
|
$guard = prev($keys); |
54
|
|
|
|
55
|
|
|
$values = $values->toArray(); |
56
|
|
|
foreach ($values as $value) { |
57
|
|
|
$item = array_only($value, $this->filed); |
|
|
|
|
58
|
|
|
$attr = json_encode(array_except($value, $this->filed)); |
|
|
|
|
59
|
|
|
$insert = array_merge($item, ['attributes' => $attr, 'key' => $key, 'guard' => $guard, 'user_id' => $userId]); |
60
|
|
|
if (DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']])->first()) { |
61
|
|
|
DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']]) |
62
|
|
|
->update(array_except($insert, ['key', '__raw_id'])); |
|
|
|
|
63
|
|
|
} else { |
64
|
|
|
DB::table($this->table)->insert($insert); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $key |
71
|
|
|
* @param null $default |
72
|
|
|
* |
73
|
|
|
* @return Collection |
74
|
|
|
*/ |
75
|
|
|
public function get($key, $default = null) |
76
|
|
|
{ |
77
|
|
|
$items = DB::table($this->table)->where('key', $key)->get(); |
78
|
|
|
|
79
|
|
|
$items = $items->toArray(); |
80
|
|
|
$collection = []; |
81
|
|
|
foreach ($items as $item) { |
82
|
|
|
$item = json_decode(json_encode($item), true); |
83
|
|
|
$attr = json_decode($item['attributes'], true); |
84
|
|
|
$item = array_only($item, $this->filed); |
|
|
|
|
85
|
|
|
$item = array_merge($item, $attr); |
86
|
|
|
$collection[$item['__raw_id']] = new Item($item); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new Collection($collection); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $key |
94
|
|
|
*/ |
95
|
|
|
public function forget($key) |
96
|
|
|
{ |
97
|
|
|
DB::table($this->table)->where('key', $key)->delete(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.