|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
|
8
|
|
|
use Thinktomorrow\AssetLibrary\Models\Asset; |
|
9
|
|
|
|
|
10
|
|
|
trait AssetTrait |
|
11
|
|
|
{ |
|
12
|
|
|
use HasMediaTrait; |
|
13
|
|
|
|
|
14
|
52 |
|
public static function bootAssetTrait() |
|
15
|
|
|
{ |
|
16
|
|
|
static::deleted(function ($model) { |
|
|
|
|
|
|
17
|
|
|
$model->assetRelation->each(function ($asset) use ($model) { |
|
18
|
1 |
|
$model->assetRelation()->updateExistingPivot($asset->id, ['unused'=> true]); |
|
19
|
1 |
|
}); |
|
20
|
52 |
|
}); |
|
21
|
52 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
52 |
|
public function assetRelation(): MorphToMany |
|
27
|
|
|
{ |
|
28
|
52 |
|
return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order'); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
40 |
|
public function asset(string $type = '', ?string $locale = null): ?Asset |
|
32
|
|
|
{ |
|
33
|
40 |
|
$this->load('assetRelation'); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
40 |
|
if ($this->assetRelation->first() === null || $this->assetRelation->first()->pivot === null) { |
|
36
|
4 |
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
38 |
|
$assets = $this->assetRelation; |
|
40
|
|
|
|
|
41
|
38 |
|
if ($type != '') { |
|
42
|
12 |
|
$assets = $this->assetRelation->where('pivot.type', $type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
38 |
|
if ($locale && $assets->count() > 1) { |
|
46
|
3 |
|
$assets = $assets->where('pivot.locale', $locale); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
38 |
|
return $assets->first(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
14 |
|
public function assets(string $type = '', string $locale = null): Collection |
|
53
|
|
|
{ |
|
54
|
14 |
|
$this->load('assetRelation'); |
|
55
|
|
|
|
|
56
|
14 |
|
$assets = $this->assetRelation; |
|
57
|
|
|
|
|
58
|
14 |
|
if ($type) { |
|
59
|
6 |
|
$assets = $assets->where('pivot.type', $type); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
14 |
|
if ($locale) { |
|
63
|
1 |
|
$assets = $assets->where('pivot.locale', $locale); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
14 |
|
return $assets->sortBy('pivot.order'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.