forked from eatvio/eatvio-infra
123 lines
3.0 KiB
PHP
Executable File
123 lines
3.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CyrildeWit\EloquentViewable\Contracts\Viewable;
|
|
use CyrildeWit\EloquentViewable\InteractsWithViews;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\AsCollection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
|
|
use Spatie\Sluggable\HasSlug;
|
|
use Spatie\Sluggable\SlugOptions;
|
|
|
|
class Recipe extends Model implements Viewable
|
|
{
|
|
use HasFactory;
|
|
use InteractsWithViews;
|
|
use HasSlug;
|
|
|
|
protected $casts = [
|
|
'steps' => AsCollection::class,
|
|
'ingredients' => AsCollection::class,
|
|
];
|
|
|
|
protected $withCount = [
|
|
'likes',
|
|
'comments',
|
|
];
|
|
|
|
public function getSlugOptions(): SlugOptions
|
|
{
|
|
return SlugOptions::create()
|
|
->generateSlugsFrom(['title'])
|
|
->saveSlugsTo('slug')
|
|
// ->slugsShouldBeNoLongerThan(20)
|
|
->usingSeparator('_');
|
|
// ->usingLanguage('ru')
|
|
// ->doNotGenerateSlugsOnUpdate();
|
|
}
|
|
|
|
/**
|
|
* Get the route key for the model.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getRouteKeyName()
|
|
{
|
|
return 'slug';
|
|
}
|
|
|
|
public function tags()
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
|
|
/* public function author() */
|
|
/* { */
|
|
/* return $this->belongsToMany(User::class, 'recipe_author', 'recipe_id', 'user_id'); */
|
|
/* } */
|
|
|
|
public function author(): HasOneThrough
|
|
{
|
|
return $this
|
|
->hasOneThrough(User::class, RecipeAuthor::class, 'recipe_id', 'id', 'id', 'user_id')
|
|
->withDefault(
|
|
[
|
|
'name' => 'Guest Author',
|
|
'id' => 0,
|
|
'profile_photo_url' => 'https://dev2.eatvio.com/storage/profile-photos/mW87tqy9P8O31Dauyvg8f7GowMNMsgt9J6u47T7S.jpg',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function comments()
|
|
{
|
|
return $this->hasMany(RecipeComment::class);
|
|
}
|
|
|
|
public function likes(): HasMany
|
|
{
|
|
return $this->hasMany(RecipeLike::class);
|
|
}
|
|
|
|
public function scopeNegativeComments(Builder $query)
|
|
{
|
|
return static::comments()->where('grade', '<=', '3');
|
|
}
|
|
|
|
public function scopePositiveComments(Builder $query)
|
|
{
|
|
return static::comments()->where('grade', '>', '3');
|
|
}
|
|
|
|
public function isLiked(): bool
|
|
{
|
|
$curUser = auth()->user();
|
|
if ($curUser) {
|
|
return $curUser->recipe_likes()->forRecipe($this)->count();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function removeLike(): bool
|
|
{
|
|
$curUser = auth()->user();
|
|
if ($curUser) {
|
|
return $curUser->recipe_likes()->forRecipe($this)->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function scopeContainsTag($query, $tagString): Builder
|
|
{
|
|
$tagId = Str::of($tagString)->split('/-/')->first();
|
|
|
|
return Tag::find($tagId)->recipes();
|
|
}
|
|
}
|