You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.9 KiB
155 lines
3.9 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Scopes\ZoneScope;
|
|
use Illuminate\Support\Str;
|
|
use App\Scopes\RestaurantScope;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
class Food extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $casts = [
|
|
'tax' => '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;
|
|
}
|
|
}
|
|
|