| 2 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace App\Models;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
6 |
use Illuminate\Database\Eloquent\Model;
|
|
|
7 |
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
8 |
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
9 |
use Illuminate\Support\Facades\Cache;
|
|
|
10 |
use Spatie\Sluggable\HasSlug;
|
|
|
11 |
use Spatie\Sluggable\SlugOptions;
|
|
|
12 |
|
| 415 |
lars |
13 |
/**
|
|
|
14 |
* App\Models\Item
|
|
|
15 |
*
|
|
|
16 |
* @property int $id
|
|
|
17 |
* @property int|null $manufacturer_id
|
|
|
18 |
* @property int|null $directory_id
|
|
|
19 |
* @property int|null $shipping_group_id
|
|
|
20 |
* @property string $name
|
|
|
21 |
* @property string $slug
|
|
|
22 |
* @property int $status
|
|
|
23 |
* @property float $weight
|
|
|
24 |
* @property string $description
|
|
|
25 |
* @property string $ean
|
|
|
26 |
* @property string|null $mpn
|
|
|
27 |
* @property string $kennung
|
|
|
28 |
* @property string $short_line_1
|
|
|
29 |
* @property string $created_by
|
|
|
30 |
* @property string $updated_by
|
|
|
31 |
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
32 |
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
33 |
* @property-read \App\Models\Manufacturer|null $manufacturer
|
|
|
34 |
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ItemMedium> $medium
|
|
|
35 |
* @property-read int|null $medium_count
|
|
|
36 |
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Price> $price
|
|
|
37 |
* @property-read int|null $price_count
|
|
|
38 |
* @method static \Database\Factories\ItemFactory factory($count = null, $state = [])
|
|
|
39 |
* @method static \Illuminate\Database\Eloquent\Builder|Item newModelQuery()
|
|
|
40 |
* @method static \Illuminate\Database\Eloquent\Builder|Item newQuery()
|
|
|
41 |
* @method static \Illuminate\Database\Eloquent\Builder|Item query()
|
|
|
42 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereCreatedAt($value)
|
|
|
43 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereCreatedBy($value)
|
|
|
44 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereDescription($value)
|
|
|
45 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereDirectoryId($value)
|
|
|
46 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereEan($value)
|
|
|
47 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereId($value)
|
|
|
48 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereKennung($value)
|
|
|
49 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereManufacturerId($value)
|
|
|
50 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereMpn($value)
|
|
|
51 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereName($value)
|
|
|
52 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereShippingGroupId($value)
|
|
|
53 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereShortLine1($value)
|
|
|
54 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereSlug($value)
|
|
|
55 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereStatus($value)
|
|
|
56 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereUpdatedAt($value)
|
|
|
57 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereUpdatedBy($value)
|
|
|
58 |
* @method static \Illuminate\Database\Eloquent\Builder|Item whereWeight($value)
|
|
|
59 |
* @mixin \Eloquent
|
|
|
60 |
*/
|
|
|
61 |
class Item extends Model
|
| 2 |
lars |
62 |
{
|
|
|
63 |
use HasFactory, HasSlug;
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Get the options for generating the slug.
|
|
|
67 |
*/
|
|
|
68 |
public function getSlugOptions(): SlugOptions
|
|
|
69 |
{
|
|
|
70 |
return SlugOptions::create()
|
|
|
71 |
->generateSlugsFrom( 'name' )
|
|
|
72 |
->saveSlugsTo( 'slug' )
|
|
|
73 |
->usingLanguage( 'de' )
|
|
|
74 |
->doNotGenerateSlugsOnUpdate()
|
|
|
75 |
->skipGenerateWhen( function ()
|
|
|
76 |
{
|
|
|
77 |
return $this->directory_id < 0;
|
|
|
78 |
} );
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
function medium(): HasMany
|
|
|
82 |
{
|
|
|
83 |
return $this->hasMany( ItemMedium::class )->orderBy( "rank" );
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
public function price(): HasMany
|
|
|
87 |
{
|
|
|
88 |
return $this->hasMany( Price::class )->where("preis","!=",0)->orderBy( "preis_index" );
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function manufacturer(): BelongsTo
|
|
|
92 |
{
|
|
|
93 |
return $this->belongsTo( Manufacturer::class );
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// On the Model class add the following method.
|
|
|
97 |
|
|
|
98 |
public function resolveRouteBinding( $value, $field = null ): ?Model
|
|
|
99 |
{
|
|
|
100 |
$cacheName = "item_" . $value;
|
|
|
101 |
|
|
|
102 |
if ( Cache::has( $cacheName ) )
|
|
|
103 |
{
|
|
|
104 |
return Cache::get( $cacheName );
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$result = $this->with( 'price', 'medium.medium', 'manufacturer' )->where( 'slug', $value )->firstOrFail();
|
|
|
108 |
Cache::put( $cacheName, $result, 86400 );
|
|
|
109 |
|
|
|
110 |
return $result;
|
|
|
111 |
}
|
|
|
112 |
}
|