'boolean' ]; protected $enumCasts = [ 'type' => RecipeType::class, 'difficulty' => RecipeDifficulty::class ]; protected $fillable = [ 'title', 'seo_title', 'seo_description', 'title', 'catch_phrase', 'tags', 'picture', 'ingredients', 'preparation', 'advice', 'keywords', 'type', 'video_link', 'preparation_time', 'cooking_time', 'number_people', 'difficulty', 'status' ]; protected static function booted() { static::creating(function ($recipe) { $recipe->slug = Str::slug($recipe->title); $recipe->ingredients = clean($recipe->ingredients); $recipe->preparation = clean($recipe->preparation); $recipe->advice = clean($recipe->advice); }); static::updating(function ($recipe) { $recipe->slug = Str::slug($recipe->title); $recipe->ingredients = clean($recipe->ingredients); $recipe->preparation = clean($recipe->preparation); $recipe->advice = clean($recipe->advice); }); } public function articles() { return $this->belongsToMany(Articles::class, 'recipes_articles', 'recipe_id', 'article_id'); } public function products() { return $this->belongsToMany(Products::class, 'recipes_products', 'recipe_id', 'product_id'); } /** * Get Associated Recipes * * @return \Illuminate\Database\Eloquent\Collection */ public function associatedRecipes() { $recipesLimit = 3; $rangeID = $this->products()->first()->range->id ?? 0; $preparationTime = $this->preparation_time ?? 0; $cookingTime = $this->cooking_time ?? 0; $recipes = Recipes::where('id', '!=', $this->id) ->where('status', true) ->whereHas('products.range', function ($q) use ($rangeID) { $q->where('ranges.id', $rangeID); })->inRandomOrder()->limit($recipesLimit)->get(); // With preparation time if (count($recipes) < $recipesLimit) { $recipes = $recipes->merge(Recipes::where('preparation_time', $preparationTime) ->where('status', true) ->inRandomOrder() ->limit($recipesLimit - count($recipes)) ->get()); } // With cooking time if (count($recipes) < $recipesLimit) { $recipes = $recipes->merge(Recipes::where('cooking_time', $cookingTime) ->where('status', true) ->inRandomOrder() ->limit($recipesLimit - count($recipes)) ->get()); } // Random if (count($recipes) < $recipesLimit) { $recipes = $recipes->merge(Recipes::limit($recipesLimit - count($recipes)) ->where('status', true) ->inRandomOrder() ->limit($recipesLimit - count($recipes)) ->get()); } return $recipes; } public function rating() { return $this->hasOne(Rating::class); } public function customers() { return $this->belongsToMany(Customers::class); } public function consumptionTimes() { return $this->belongsTo(ConsumptionTimes::class, 'consumption_times_id'); } public function notifiable() { return $this->morphOne(Notifications::class, 'notifiable'); } /** * Check if recipe has been rated by the user (in session also if not Auth) * * @return boolean */ public function hasBeenRatedByUser() { if (auth('customer')->check()) { /** @var \App\Models\Customers $customer */ $customer = auth('customer')->user(); return is_null($customer->getRecipeRating($this)) ? false : true; } else { return session()->has(sprintf('recipes.rating.%s', $this->slug)); } } /** * Check if recipe has been rated by the user (in session also if not Auth) * * @return boolean */ public function getUserRating() { if (auth('customer')->check()) { /** @var \App\Models\Customers $customer */ $customer = auth('customer')->user(); return $customer->getRecipeRating($this); } else { return session()->get(sprintf('recipes.rating.%s', $this->slug)); } } /** * Set the recipes rating and memorize in session, or user * * @param string $rating * @return void */ public function setRateForUserAndSave($rating) { $total = ($this->rating->total ?? 0) + $rating; $count = ($this->rating->count ?? 0) + 1; $o = [ 'total' => $total, 'count' => $count, 'rating' => $count ? floatval($total / $count) : 0 ]; if (!$this->rating()->exists()) { $this->rating()->save(new Rating($o)); } else { $this->rating->fill($o); $this->rating->save(); } if (auth('customer')->check()) { /** @var \App\Models\Customers $customer */ $customer = auth('customer')->user(); $customer->setRecipeRating($this, $rating); } else { session()->put(sprintf('recipes.rating.%s', $this->slug), $rating); session()->save(); } } }