forked from eatvio/eatvio-infra
191 lines
6.1 KiB
PHP
191 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CyrildeWit\EloquentViewable\Contracts\Viewable;
|
|
use CyrildeWit\EloquentViewable\InteractsWithViews;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/**
|
|
* App\Models\Diet
|
|
*
|
|
* @property int $id
|
|
* @property string $name name of diet
|
|
* @property string|null $image public path to image
|
|
* @property int $duration diet duration - 7, 14, 28 days
|
|
* @property string $state active/draft/archived
|
|
* @property mixed $type has meat/vegs/carb/alcohol
|
|
* @property string $details diet details
|
|
* @property mixed $daily_schedule A list of planned foods
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
*
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $author
|
|
* @property-read int|null $author_count
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ChosenDiet> $choses
|
|
* @property-read int|null $choses_count
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\DietComment> $comments
|
|
* @property-read int|null $comments_count
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\DietLike> $likes
|
|
* @property-read int|null $likes_count
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \CyrildeWit\EloquentViewable\View> $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 $casts = [
|
|
'type' => 'array',
|
|
'daily_schedule' => 'array',
|
|
];
|
|
|
|
private array $allowedDietTypes = [
|
|
'alko',
|
|
'corb',
|
|
'meat',
|
|
'veg',
|
|
];
|
|
|
|
// protected $withCount = [
|
|
// 'likes',
|
|
// 'comments',
|
|
// 'choses',
|
|
// ];
|
|
|
|
public static function getWithLikesCommentsChosesCount(): Builder
|
|
{
|
|
return self::withCount(['likes', 'comments', 'choses']);
|
|
}
|
|
|
|
public function author(): BelongsToMany
|
|
{
|
|
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(): HasMany
|
|
{
|
|
return $this->hasMany(DietComment::class);
|
|
}
|
|
|
|
public function likes(): HasMany
|
|
{
|
|
return $this->hasMany(DietLike::class);
|
|
}
|
|
|
|
public function isLiked(): bool|int
|
|
{
|
|
if (auth()->user()) {
|
|
return auth()->user()->diet_likes()->forDiet($this)->count();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function removeLike(): bool|int
|
|
{
|
|
if (auth()->user()) {
|
|
return auth()->user()->diet_likes()->forDiet($this)->delete();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function types(): Attribute
|
|
{
|
|
$data = $this->type;
|
|
|
|
return new Attribute(
|
|
get: fn () => array_keys($data),
|
|
);
|
|
}
|
|
|
|
public function schedules(): Attribute
|
|
{
|
|
$data = $this->daily_schedule;
|
|
|
|
return new Attribute(
|
|
get: fn () => $data,
|
|
);
|
|
}
|
|
|
|
public function choses(): HasMany
|
|
{
|
|
return $this->hasMany(ChosenDiet::class);
|
|
}
|
|
|
|
public function scopeDurationMin(Builder $query, string $days): Builder
|
|
{
|
|
return $query->where('duration', '>=', $days);
|
|
}
|
|
|
|
public function scopeDurationMax(Builder $query, string $days): Builder
|
|
{
|
|
return $query->where('duration', '<=', $days);
|
|
}
|
|
|
|
public function scopeInvolvedMin(Builder $query, string $number): Builder
|
|
{
|
|
return $query->having('choses_count', '>=', $number);
|
|
}
|
|
|
|
public function scopeInvolvedMax(Builder $query, string $number): Builder
|
|
{
|
|
return $query->having('choses_count', '<=', $number);
|
|
}
|
|
|
|
public function scopeContainsType(Builder $query, array $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;
|
|
}
|
|
}
|