$author * @property-read int|null $author_count * @property-read \Illuminate\Database\Eloquent\Collection $choses * @property-read int|null $choses_count * @property-read \Illuminate\Database\Eloquent\Collection $comments * @property-read int|null $comments_count * @property-read \Illuminate\Database\Eloquent\Collection $likes * @property-read int|null $likes_count * @property-read \Illuminate\Database\Eloquent\Collection $views * @property-read int|null $views_count * * @method static Builder|Diet containsType($types) * @method static Builder|Diet durationMax($days) * @method static Builder|Diet durationMin($days) * @method static \Database\Factories\DietFactory factory($count = null, $state = []) * @method static Builder|Diet involvedMax($number) * @method static Builder|Diet involvedMin($number) * @method static Builder|Diet newModelQuery() * @method static Builder|Diet newQuery() * @method static Builder|Diet orderByUniqueViews(string $direction = 'desc', $period = null, ?string $collection = null, string $as = 'unique_views_count') * @method static Builder|Diet orderByViews(string $direction = 'desc', ?\CyrildeWit\EloquentViewable\Support\Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count') * @method static Builder|Diet query() * @method static Builder|Diet whereCreatedAt($value) * @method static Builder|Diet whereDailySchedule($value) * @method static Builder|Diet whereDetails($value) * @method static Builder|Diet whereDuration($value) * @method static Builder|Diet whereId($value) * @method static Builder|Diet whereImage($value) * @method static Builder|Diet whereName($value) * @method static Builder|Diet whereState($value) * @method static Builder|Diet whereType($value) * @method static Builder|Diet whereUpdatedAt($value) * @method static Builder|Diet withViewsCount(?\CyrildeWit\EloquentViewable\Support\Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count') * * @mixin \Eloquent */ class Diet extends Model implements Viewable { use HasFactory, InteractsWithViews; protected $withCount = [ 'likes', 'comments', 'choses', ]; private $allowedDietTypes = [ 'alko', 'corb', 'meat', 'veg', ]; public function author() { return $this ->belongsToMany(User::class, 'diet_author', 'diet_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(DietComment::class); } public function likes(): HasMany { return $this->hasMany(DietLike::class); } public function isLiked(): bool { if (auth()->user()) { return auth()->user()->diet_likes()->forDiet($this)->count(); } return false; } public function removeLike(): bool { if (auth()->user()) { return auth()->user()->diet_likes()->forDiet($this)->delete(); } return false; } public function types(): Attribute { $data = $this->unserializeOptionsArr($this->type); return new Attribute( get: fn () => array_keys($data), ); } public function schedules(): Attribute { $data = $this->unserializeOptionsArr($this->daily_schedule); return new Attribute( get: fn () => $data, ); } public function choses() { return $this->hasMany(ChosenDiet::class); } public function scopeDurationMin(Builder $query, $days): Builder { return $query->where('duration', '>=', $days); } public function scopeDurationMax(Builder $query, $days): Builder { return $query->where('duration', '<=', $days); } public function scopeInvolvedMin(Builder $query, $number): Builder { return $query->having('choses_count', '>=', $number); } public function scopeInvolvedMax(Builder $query, $number): Builder { return $query->having('choses_count', '<=', $number); } public function scopeContainsType(Builder $query, $types): Builder { $i = 0; foreach ($types as $type) { if (in_array($type, $this->allowedDietTypes)) { $query = $i === 0 ? $query->whereJsonContainsKey('type->'.$type) : $query->orWhereJsonContainsKey('type->'.$type); $i++; } } return $query; } private function unserializeOptionsArr($str) { if (preg_match('/^".*"$/', $str)) { return json_decode(stripslashes(mb_substr($str, 1, -1)), true); } return json_decode(stripslashes($str), true); } }