*/ protected $fillable = [ 'title', 'video_id', 'product_id', 'is_on_homepage', 'order', 'published', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'published' => 'boolean', 'is_on_homepage' => 'boolean', ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $video) { self::setOrder($video); }); static::created(function () { self::resetCache(); }); static::updated(function () { self::resetCache(); }); static::deleted(function () { self::resetCache(); }); } // * METHODS /** * Set order after the last element of the list. * * @param self $model * @return void */ private static function setOrder(self $model): void { $model->order = \intval(self::query()->max('order')) + 1; } /** * Reset all cache elements related to this model. * * @return void */ private static function resetCache(): void { Cache::forget('9publishedHomeVideosOrderedByOrderWithProducts'); self::get9PublishedHomeVideosOrderedWithProducts(); Cache::forget('6publishedVideosOrderedByOrderWithProducts'); self::get6PublishedVideosOrderedWithProducts(); } /** * Get 9 published Videos to display in homepage ordered by order with products relations. * * @return \Illuminate\Database\Eloquent\Collection */ public static function get9PublishedHomeVideosOrderedWithProducts(): \Illuminate\Database\Eloquent\Collection { return Cache::rememberForever('9publishedHomeVideosOrderedByOrderWithProducts', function () { return self::query() ->where('published', true) ->where('is_on_homepage', true) ->orderBy('order') ->with('product') ->limit(9) ->get(); }); } /** * Get 9 published Videos to display in homepage ordered by order with products relations. * * @return \Illuminate\Database\Eloquent\Collection */ public static function get6PublishedVideosOrderedWithProducts(): \Illuminate\Database\Eloquent\Collection { return Cache::rememberForever('6publishedVideosOrderedByOrderWithProducts', function () { return self::query() ->where('published', true) ->orderBy('order') ->with('product') ->take(6) ->get(); }); } // * RELATIONSHIPS /** * Get the product for the video. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Product::class); } }