'float', 'price' => 'float', 'status' => 'integer', 'discount' => 'float', 'avg_rating' => 'float', 'set_menu' => 'integer', 'category_id' => 'integer', 'restaurant_id' => 'integer', 'reviews_count' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'veg' => 'integer', 'min' => 'integer', 'max' => 'integer', ]; public function scopeRecommended($query) { return $query->where('recommended',1); } public function translations() { return $this->morphMany(Translation::class, 'translationable'); } public function scopeActive($query) { return $query->where('status', 1)->whereHas('restaurant', function ($query) { return $query->where('status', 1); }); } public function scopeAvailable($query,$time) { $query->where(function($q)use($time){ $q->where('available_time_starts','<=',$time)->where('available_time_ends','>=',$time); }); } public function scopePopular($query) { return $query->orderBy('order_count', 'desc'); } public function reviews() { return $this->hasMany(Review::class)->latest(); } public function rating() { return $this->hasMany(Review::class) ->select(DB::raw('avg(rating) average, count(food_id) rating_count, food_id')) ->groupBy('food_id'); } public function restaurant() { return $this->belongsTo(Restaurant::class); } public function category() { return $this->belongsTo(Category::class, 'category_id'); } public function orders() { return $this->hasMany(OrderDetail::class); } public function getCategoryAttribute() { $category = Category::find(json_decode($this->category_ids)[0]->id); return $category ? $category->name : translate('messages.uncategorize'); } protected static function booted() { if (auth('vendor')->check() || auth('vendor_employee')->check()) { static::addGlobalScope(new RestaurantScope); } static::addGlobalScope(new ZoneScope); static::addGlobalScope('translate', function (Builder $builder) { $builder->with(['translations' => function ($query) { return $query->where('locale', app()->getLocale()); }]); }); } public function scopeType($query, $type) { if ($type == 'veg') { return $query->where('veg', true); } else if ($type == 'non_veg') { return $query->where('veg', false); } return $query; } public function tags() { return $this->belongsToMany(Tag::class); } protected static function boot() { parent::boot(); static::created(function ($food) { $food->slug = $food->generateSlug($food->name); $food->save(); }); } private function generateSlug($name) { $slug = Str::slug($name); if ($max_slug = static::where('slug', 'like',"{$slug}%")->latest('id')->value('slug')) { if($max_slug == $slug) return "{$slug}-2"; $max_slug = explode('-',$max_slug); $count = array_pop($max_slug); if (isset($count) && is_numeric($count)) { $max_slug[]= ++$count; return implode('-', $max_slug); } } return $slug; } }