$author * @property-read int|null $author_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 \Database\Factories\PlanFactory factory($count = null, $state = []) * @method static Builder|Plan negativeComments() * @method static Builder|Plan newModelQuery() * @method static Builder|Plan newQuery() * @method static Builder|Plan orderByUniqueViews(string $direction = 'desc', $period = null, ?string $collection = null, string $as = 'unique_views_count') * @method static Builder|Plan orderByViews(string $direction = 'desc', ?\CyrildeWit\EloquentViewable\Support\Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count') * @method static Builder|Plan positiveComments() * @method static Builder|Plan query() * @method static Builder|Plan whereCreatedAt($value) * @method static Builder|Plan whereDetails($value) * @method static Builder|Plan whereDuration($value) * @method static Builder|Plan whereId($value) * @method static Builder|Plan whereImage($value) * @method static Builder|Plan whereName($value) * @method static Builder|Plan wherePacket1Options($value) * @method static Builder|Plan wherePacket2Options($value) * @method static Builder|Plan wherePacket3Options($value) * @method static Builder|Plan wherePacketsCount($value) * @method static Builder|Plan whereSlug($value) * @method static Builder|Plan whereState($value) * @method static Builder|Plan whereType($value) * @method static Builder|Plan whereUpdatedAt($value) * @method static Builder|Plan withViewsCount(?\CyrildeWit\EloquentViewable\Support\Period $period = null, ?string $collection = null, bool $unique = false, string $as = 'views_count') * * @mixin \Eloquent */ class Plan extends Model implements Viewable { use HasFactory; use HasSlug; use InteractsWithViews; use PresentableTrait; protected $withCount = [ 'likes', 'comments', ]; protected $casts = [ 'type' => 'array', 'packet1_options' => 'array', 'packet2_options' => 'array', 'packet3_options' => 'array', ]; protected string $presenter = PlanPresenter::class; public function getSlugOptions(): SlugOptions { return SlugOptions::create() ->generateSlugsFrom(['name']) ->saveSlugsTo('slug') ->usingSeparator('_'); } public function getRouteKeyName(): string { return 'slug'; } public function author(): BelongsToMany { return $this->belongsToMany(User::class, 'plan_author', 'plan_id', 'user_id'); } public function comments(): HasMany { return $this->hasMany(PlanComment::class); } public function likes(): HasMany { return $this->hasMany(PlanLike::class); } public function isLiked(): bool|int { if (auth()->user()) { return auth()->user()->plan_likes()->forPlan($this)->count(); } return false; } public function allItems(): array { $result = []; for ($i = 1; $i <= $this->packets_count; $i++) { $key = 'packet'.$i.'_options'; $data = $this->$key; if (! is_null($data)) { $result = array_merge($result, data_get($data, 'items', [])); } } return array_values(array_unique($result)); } public function types(): Attribute { $data = $this->type; return new Attribute( get: fn () => array_keys($data), ); } public function minPrice(): Attribute { $result = 0; for ($i = 1; $i <= $this->packets_count; $i++) { $key = 'packet'.$i.'_options'; $data = $this->$key; if (! is_null($data)) { $curPrice = (int) data_get($data, 'price', 0); $result = $curPrice > $result ? $curPrice : $result; } } return new Attribute( get: fn () => $result, ); } public function removeLike(): bool { if (auth()->user()) { return auth()->user()->plan_likes()->forPlan($this)->delete(); } return false; } public function scopeNegativeComments(Builder $query): HasMany { return static::comments()->where('grade', '<=', '3'); } public function scopePositiveComments(Builder $query): HasMany { return static::comments()->where('grade', '>', '3'); } }