Files
eatvio-infra/app/Models/Diary.php
2023-10-04 17:57:09 +03:00

48 lines
1.0 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Meal;
use Illuminate\Database\Eloquent\Casts\Attribute;
class Diary extends Model
{
use HasFactory;
// protected $casts = ['date' => 'datetime:d-m-Y'];
// protected $dateFormat = 'd-m-Y';
public function user()
{
return $this->belongsTo(User::class)->withDefault([
'name' => 'Guest Author',
]);
}
public function meals()
{
return $this->hasMany(Meal::class);
}
public function scopeUserToDayItem($query, $userId)
{
return $query
->where('user_id', $userId)
->where('date', now()->format('Y-m-d'));
}
public function consultant_comments()
{
return $this->hasMany(ConsultantDiaryComment::class);
}
protected function hasComments(): Attribute
{
return Attribute::make(
get: fn () => $this->consultant_comments()->count() > 0,
);
}
}