Skip to content

Commit 58c9e69

Browse files
Refactor HasStoredAssets: adjust boot behavior to defer initialization, update type hints in Product model.
1 parent 7911d8e commit 58c9e69

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/StoredAssets/HasStoredAssets.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ trait HasStoredAssets
1414

1515
protected static function bootHasStoredAssets(): void
1616
{
17-
self::initStoredAssetFieldsAndRelations();
17+
18+
self::whenBooted(function () {
19+
self::initStoredAssetFieldsAndRelations();
20+
});
1821
self::saving(function (Model $model) {
1922
foreach ($model->getDirty() as $field => $item) {
2023
if (! $item instanceof Recipe) {
@@ -52,7 +55,7 @@ public function setAttribute($key, $value)
5255
{
5356
if (isset(self::$storedAssetFields[$key])) {
5457
if ($this->hasCast($key)) {
55-
/** @var class-string<\Laraveltoolkit\StoredAssets\Recipe> $cast */
58+
/** @var class-string<Recipe> $cast */
5659
$cast = $this->casts()[$key];
5760
$value = $cast::parse($this, $key, $value);
5861
}

tests/Model/Product.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
namespace Laraveltoolkit\Tests\Model;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Carbon;
7+
use Laraveltoolkit\StoredAssets\Assets;
68
use Laraveltoolkit\StoredAssets\HasStoredAssets;
79

810
/**
911
* @property int $id
1012
* @property int|null $user_id
11-
* @property string|\Laraveltoolkit\StoredAssets\Assets $image
12-
* @property \Illuminate\Support\Carbon $created_at
13-
* @property \Illuminate\Support\Carbon $updated_at
13+
* @property string|Assets $image
14+
* @property Carbon $created_at
15+
* @property Carbon $updated_at
1416
*/
1517
class Product extends Model
1618
{
1719
use HasStoredAssets;
1820

1921
/**
20-
* The attributes that are mass assignable.
22+
* The attributes that are mass-assignable.
2123
*
2224
* @var array<int, string>
2325
*/

0 commit comments

Comments
 (0)