'boolean' ]; protected $enumCasts = [ 'type' => RecipeType::class, 'difficulty' => RecipeDifficulty::class ]; 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 range * * @return Ranges|null */ public function associatedRange() { return $this->products()->first()->range; } /** * 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) ->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) ->inRandomOrder() ->limit($recipesLimit - count($recipes)) ->get()); } // With cooking time if (count($recipes) < $recipesLimit) { $recipes = $recipes->merge(Recipes::where('cooking_time', $cookingTime) ->inRandomOrder() ->limit($recipesLimit - count($recipes)) ->get()); } // Random if (count($recipes) < $recipesLimit) { $recipes = $recipes->merge(Recipes::limit($recipesLimit - count($recipes)) ->inRandomOrder() ->limit($recipesLimit - count($recipes)) ->get()); } return $recipes; } public function rating() { return $this->hasOne(Rating::class); } public function product1() { return $this->belongsTo(Products::class, 'associated_product1'); } public function product2() { return $this->belongsTo(Products::class, 'associated_product2'); } public function customers() { return $this->belongsToMany(Customers::class); } public function consumptionTimes() { return $this->belongsTo(ConsumptionTimes::class); } /** * Check if recipe has been rated by the user (in session also if not Auth) * TODO: If User is Auth check rating for the user * @return boolean */ public function hasBeenRatedByUser() { return session()->has(sprintf('recipes.rating.%s', $this->slug)); } /** * Set the recipes rating and memorize in session, or user * TODO: If User is Auth save rating for the user * @param string $rating * @return void */ public function setRateForUserAndSave($rating) { $total = $this->rating->total + $rating; $count = ++$this->rating->count; $o = [ 'total' => $total, 'count' => $count, 'rating' => floatval($total / $count) ]; if (!$this->rating->exists()) { $this->attach(new Rating($o)); } else { $this->rating->total = $o['total']; $this->rating->rating = $o['rating']; $this->rating->count = $o['count']; $this->rating->save(); } session()->put(sprintf('recipes.rating.%s', $this->slug), $rating); session()->save(); } }