Subversion-Projekte lars-tiefland.webanos.zeldi.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
4 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
 
13
    class Item extends Model
14
    {
15
        use HasFactory, HasSlug;
16
 
17
        /**
18
         * Get the options for generating the slug.
19
         */
20
        public function getSlugOptions(): SlugOptions
21
        {
22
            return SlugOptions::create()
23
                ->generateSlugsFrom( 'name' )
24
                ->saveSlugsTo( 'slug' )
25
                ->usingLanguage( 'de' )
26
                ->doNotGenerateSlugsOnUpdate()
27
                ->skipGenerateWhen( function ()
28
                {
29
                    return $this->directory_id < 0;
30
                } );
31
        }
32
 
33
        function medium(): HasMany
34
        {
35
            return $this->hasMany( ItemMedium::class )->orderBy( "rank" );
36
        }
37
 
38
        public function price(): HasMany
39
        {
40
            return $this->hasMany( Price::class )->where("preis","!=",0)->orderBy( "preis_index" );
41
        }
42
 
43
        public function manufacturer(): BelongsTo
44
        {
45
            return $this->belongsTo( Manufacturer::class );
46
        }
47
 
48
        // On the Model class add the following method.
49
 
50
        public function resolveRouteBinding( $value, $field = null ): ?Model
51
        {
52
            $cacheName = "item_" . $value;
53
 
54
            if ( Cache::has( $cacheName ) )
55
            {
56
                return Cache::get( $cacheName );
57
            }
58
 
59
            $result = $this->with( 'price', 'medium.medium', 'manufacturer' )->where( 'slug', $value )->firstOrFail();
60
            Cache::put( $cacheName, $result, 86400 );
61
 
62
            return $result;
63
        }
64
    }