Skip to content

Commit 6a4d810

Browse files
authored
Merge pull request #42 from mattyforth/fix/user-uuid
[1.x] Add support for user UUIDs
2 parents 845eb27 + 13521ec commit 6a4d810

8 files changed

Lines changed: 74 additions & 37 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Publish](#publish)
1515
- [Usage](#usage)
1616
- [Configuration](#configuration)
17+
- [User Configuration](#user-configuration)
1718
- [Laravel Cart Facade](#laravel-cart-facade)
1819
- [Driver](#driver)
1920
- [Support Drivers](#support-drivers)
@@ -93,6 +94,23 @@ After publishing, run the `php artisan migrate` command.
9394

9495
You can config the `Laravel Cart` with `laravel-cart.php` config that exists in `config` folder.
9596

97+
<a name="user-configuration"></a>
98+
#### User Configuration
99+
100+
If you are using `uuid` or `ulid`, you can change `foreign_key_type` to your correct foreign key type:
101+
102+
```php
103+
'users' => [
104+
...
105+
106+
/*
107+
* Specify the type of foreign key being used (e.g., 'id', 'uuid', 'ulid').
108+
* For non-standard IDs, make sure to add the relevant traits to your models.
109+
*/
110+
'foreign_key_type' => 'id', // Options: uuid, ulid, id
111+
],
112+
```
113+
96114
<a name="laravel-cart-facade"></a>
97115
### Laravel Cart Facade
98116

config/laravel-cart.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
* The user foreign key.
1717
*/
1818
'foreign_id' => 'user_id',
19+
20+
/*
21+
* Specify the type of foreign key being used (e.g., 'id', 'uuid', 'ulid').
22+
* For non-standard IDs, make sure to add the relevant traits to your models.
23+
*/
24+
'foreign_key_type' => 'id', // Options: uuid, ulid, id
1925
],
2026

2127
/*

database/migrations/2024_05_31_102710_create_carts_table.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,25 @@ public function up(): void
1313
{
1414
$userTableName = config('laravel-cart.users.table', 'users');
1515
$userForeignName = config('laravel-cart.users.foreign_id', 'user_id');
16+
$type = config('laravel-cart.users.foreign_key_type', 'id');
1617
$table = config('laravel-cart.carts.table', 'carts');
1718

18-
Schema::create($table, function (Blueprint $table) use ($userTableName, $userForeignName) {
19+
Schema::create($table, function (Blueprint $table) use ($userTableName, $userForeignName, $type) {
1920
$table->id();
2021

21-
$table->foreignId($userForeignName)->constrained($userTableName)->cascadeOnDelete();
22+
if ($type === 'ulid') {
23+
$table->foreignUlid($userForeignName)
24+
->constrained($userTableName)
25+
->cascadeOnDelete();
26+
} else if ($type === 'uuid') {
27+
$table->foreignUuid($userForeignName)
28+
->constrained($userTableName)
29+
->cascadeOnDelete();
30+
} else {
31+
$table->foreignId($userForeignName)
32+
->constrained($userTableName)
33+
->cascadeOnDelete();
34+
}
2235

2336
$table->timestamps();
2437
});

src/Drivers/Driver.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
interface Driver
88
{
9-
public function storeItem(Model|array $item, ?int $userId = null): Driver;
9+
public function storeItem(Model|array $item, ?string $userId = null): Driver;
1010

11-
public function storeItems(array $items, ?int $userId = null): Driver;
11+
public function storeItems(array $items, ?string $userId = null): Driver;
1212

13-
public function increaseQuantity(Model $item, int $quantity = 1, ?int $userId = null): Driver;
13+
public function increaseQuantity(Model $item, int $quantity = 1, ?string $userId = null): Driver;
1414

15-
public function decreaseQuantity(Model $item, int $quantity = 1, ?int $userId = null): Driver;
15+
public function decreaseQuantity(Model $item, int $quantity = 1, ?string $userId = null): Driver;
1616

17-
public function removeItem(Model $item, ?int $userId = null): Driver;
17+
public function removeItem(Model $item, ?string $userId = null): Driver;
1818

19-
public function emptyCart(?int $userId = null): Driver;
19+
public function emptyCart(?string $userId = null): Driver;
2020
}

src/Drivers/LaravelCartDatabase.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class LaravelCartDatabase implements Driver
1111
/**
1212
* Store item in cart.
1313
*/
14-
public function storeItem(Model|array $item, ?int $userId = null): static
14+
public function storeItem(Model|array $item, ?string $userId = null): static
1515
{
1616
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
1717
$cart->storeItem($item);
@@ -22,7 +22,7 @@ public function storeItem(Model|array $item, ?int $userId = null): static
2222
/**
2323
* Store multiple items in cart.
2424
*/
25-
public function storeItems(array $items, ?int $userId = null): static
25+
public function storeItems(array $items, ?string $userId = null): static
2626
{
2727
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
2828
$cart->storeItems($items);
@@ -33,7 +33,7 @@ public function storeItems(array $items, ?int $userId = null): static
3333
/**
3434
* Increase the quantity of the item.
3535
*/
36-
public function increaseQuantity(Model $item, int $quantity = 1, ?int $userId = null): static
36+
public function increaseQuantity(Model $item, int $quantity = 1, ?string $userId = null): static
3737
{
3838
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
3939
$item = $cart->items()->firstWhere('itemable_id', $item->getKey());
@@ -50,7 +50,7 @@ public function increaseQuantity(Model $item, int $quantity = 1, ?int $userId =
5050
/**
5151
* Decrease the quantity of the item.
5252
*/
53-
public function decreaseQuantity(Model $item, int $quantity = 1, ?int $userId = null): static
53+
public function decreaseQuantity(Model $item, int $quantity = 1, ?string $userId = null): static
5454
{
5555
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
5656
$item = $cart->items()->firstWhere('itemable_id', $item->getKey());
@@ -67,7 +67,7 @@ public function decreaseQuantity(Model $item, int $quantity = 1, ?int $userId =
6767
/**
6868
* Remove a single item from the cart
6969
*/
70-
public function removeItem(Model $item, ?int $userId = null): static
70+
public function removeItem(Model $item, ?string $userId = null): static
7171
{
7272
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
7373
$itemToDelete = $cart->items()->find($item->getKey());
@@ -79,7 +79,7 @@ public function removeItem(Model $item, ?int $userId = null): static
7979
/**
8080
* Remove every item from the cart
8181
*/
82-
public function emptyCart(?int $userId = null): static
82+
public function emptyCart(?string $userId = null): static
8383
{
8484
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
8585
$cart->emptyCart();
@@ -90,7 +90,7 @@ public function emptyCart(?int $userId = null): static
9090
/**
9191
* Get option for item.
9292
*/
93-
public function getOption(string $option, ?int $itemId = null, ?int $userId = null): mixed
93+
public function getOption(string $option, ?int $itemId = null, ?string $userId = null): mixed
9494
{
9595
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
9696
$items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) {
@@ -103,7 +103,7 @@ public function getOption(string $option, ?int $itemId = null, ?int $userId = nu
103103
/**
104104
* Get all options of one item.
105105
*/
106-
public function getOptions(?int $itemId = null, ?int $userId = null): mixed
106+
public function getOptions(?int $itemId = null, ?string $userId = null): mixed
107107
{
108108
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
109109
$items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) {
@@ -116,7 +116,7 @@ public function getOptions(?int $itemId = null, ?int $userId = null): mixed
116116
/**
117117
* Set option for item.
118118
*/
119-
public function setOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null): static
119+
public function setOption(string $key, mixed $value, ?int $itemId = null, ?string $userId = null): static
120120
{
121121
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
122122
$items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) {
@@ -131,7 +131,7 @@ public function setOption(string $key, mixed $value, ?int $itemId = null, ?int $
131131
/**
132132
* Get option for item.
133133
*/
134-
public function addOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null): static
134+
public function addOption(string $key, mixed $value, ?int $itemId = null, ?string $userId = null): static
135135
{
136136
$cart = Cart::query()->firstOrCreate(['user_id' => $this->resolveUserId($userId)]);
137137
$items = $cart->items()->when(! is_null($itemId), function (Builder $builder) use ($itemId) {
@@ -146,7 +146,7 @@ public function addOption(string $key, mixed $value, ?int $itemId = null, ?int $
146146
/**
147147
* Resolve the user ID, defaulting to the authenticated user.
148148
*/
149-
protected function resolveUserId(?int $userId): int
149+
protected function resolveUserId(?string $userId): string
150150
{
151151
return $userId ?? auth()->id();
152152
}

src/Drivers/LaravelCartSession.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class LaravelCartSession implements Driver
1212
/**
1313
* Store item in cart.
1414
*/
15-
public function storeItem(Model|array $item, ?int $userId = null): static
15+
public function storeItem(Model|array $item, ?string $userId = null): static
1616
{
1717
$userId = $this->resolveUserId($userId);
1818
$cart = $this->getCart($userId);
@@ -43,7 +43,7 @@ public function storeItem(Model|array $item, ?int $userId = null): static
4343
/**
4444
* Store multiple items in cart.
4545
*/
46-
public function storeItems(array $items, ?int $userId = null): static
46+
public function storeItems(array $items, ?string $userId = null): static
4747
{
4848
foreach ($items as $item) {
4949
$this->storeItem($item, $userId);
@@ -55,7 +55,7 @@ public function storeItems(array $items, ?int $userId = null): static
5555
/**
5656
* Increase the quantity of the item.
5757
*/
58-
public function increaseQuantity(Model $item, int $quantity = 1, ?int $userId = null): static
58+
public function increaseQuantity(Model $item, int $quantity = 1, ?string $userId = null): static
5959
{
6060
$userId = $this->resolveUserId($userId);
6161
$cart = $this->getCart($userId);
@@ -75,7 +75,7 @@ public function increaseQuantity(Model $item, int $quantity = 1, ?int $userId =
7575
/**
7676
* Decrease the quantity of the item.
7777
*/
78-
public function decreaseQuantity(Model $item, int $quantity = 1, ?int $userId = null): static
78+
public function decreaseQuantity(Model $item, int $quantity = 1, ?string $userId = null): static
7979
{
8080
$userId = $this->resolveUserId($userId);
8181
$cart = $this->getCart($userId);
@@ -95,7 +95,7 @@ public function decreaseQuantity(Model $item, int $quantity = 1, ?int $userId =
9595
/**
9696
* Remove a single item from the cart.
9797
*/
98-
public function removeItem(Model $item, ?int $userId = null): static
98+
public function removeItem(Model $item, ?string $userId = null): static
9999
{
100100
$userId = $this->resolveUserId($userId);
101101
$cart = $this->getCart($userId);
@@ -109,7 +109,7 @@ public function removeItem(Model $item, ?int $userId = null): static
109109
/**
110110
* Remove every item from the cart.
111111
*/
112-
public function emptyCart(?int $userId = null): static
112+
public function emptyCart(?string $userId = null): static
113113
{
114114
$userId = $this->resolveUserId($userId);
115115
session([$this->sessionKey($userId) => []]);
@@ -120,7 +120,7 @@ public function emptyCart(?int $userId = null): static
120120
/**
121121
* Get the cart from the session.
122122
*/
123-
protected function getCart(?int $userId = null): array
123+
protected function getCart(?string $userId = null): array
124124
{
125125
$userId = $this->resolveUserId($userId);
126126

@@ -130,15 +130,15 @@ protected function getCart(?int $userId = null): array
130130
/**
131131
* Resolve the session key for a user.
132132
*/
133-
protected function sessionKey(int $userId): string
133+
protected function sessionKey(string $userId): string
134134
{
135135
return self::SESSION_KEY_PREFIX.$userId;
136136
}
137137

138138
/**
139139
* Resolve the user ID, defaulting to the authenticated user.
140140
*/
141-
protected function resolveUserId(?int $userId): int
141+
protected function resolveUserId(?string $userId): string
142142
{
143143
return $userId ?? auth()->id();
144144
}

src/LaravelCart.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
/**
66
* @method static \Binafy\LaravelCart\Drivers\Driver driver(string|null $driver = null)
7-
* @method static \Binafy\LaravelCart\Drivers\Driver storeItem(\Illuminate\Database\Eloquent\Model|array $item, int|null $userId = null)
7+
* @method static \Binafy\LaravelCart\Drivers\Driver storeItem(\Illuminate\Database\Eloquent\Model|array $item, string|null $userId = null)
88
* @method static \Binafy\LaravelCart\Drivers\Driver storeItems(array $items)
99
* @method static \Binafy\LaravelCart\Drivers\Driver increaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1)
1010
* @method static \Binafy\LaravelCart\Drivers\Driver decreaseQuantity(\Illuminate\Database\Eloquent\Model $item, int $quantity = 1)
1111
* @method static \Binafy\LaravelCart\Drivers\Driver removeItem(\Illuminate\Database\Eloquent\Model $item)
1212
* @method static \Binafy\LaravelCart\Drivers\Driver emptyCart()
13-
* @method static \Binafy\LaravelCart\Drivers\Driver getOption(string $option, ?int $itemId = null, ?int $userId = null)
14-
* @method static \Binafy\LaravelCart\Drivers\Driver getOptions(?int $itemId = null, ?int $userId = null)
15-
* @method static \Binafy\LaravelCart\Drivers\Driver setOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null)
16-
* @method static \Binafy\LaravelCart\Drivers\Driver addOption(string $key, mixed $value, ?int $itemId = null, ?int $userId = null)
13+
* @method static \Binafy\LaravelCart\Drivers\Driver getOption(string $option, ?int $itemId = null, ?string $userId = null)
14+
* @method static \Binafy\LaravelCart\Drivers\Driver getOptions(?int $itemId = null, ?string $userId = null)
15+
* @method static \Binafy\LaravelCart\Drivers\Driver setOption(string $key, mixed $value, ?int $itemId = null, ?string $userId = null)
16+
* @method static \Binafy\LaravelCart\Drivers\Driver addOption(string $key, mixed $value, ?int $itemId = null, ?string $userId = null)
1717
* @method static string getDefaultDriver()
1818
* @method static void setDefaultDriver(string $name)
1919
*

src/Models/Cart.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
class Cart extends Model
1515
{
1616
/**
17-
* Fillable columns.
17+
* Guarded columns.
1818
*
19-
* @var string[]
19+
* @var array
2020
*/
21-
protected $fillable = ['user_id'];
21+
protected $guarded = ['id'];
2222

2323
/**
2424
* The relations to eager load on every query.
@@ -56,7 +56,7 @@ public function scopeFirstOrCreateWithStoreItems(
5656
Builder $query,
5757
Model $item,
5858
int $quantity = 1,
59-
?int $userId = null
59+
?string $userId = null
6060
): Builder {
6161
if (is_null($userId)) {
6262
$userId = auth()->id();

0 commit comments

Comments
 (0)