试图从 maxid 查询的变量中获取星期几

Trying to get day of week from variable queried by maxid

我试图从 maxid 找到的变量中获取星期几,但我只收到此错误:

Trying to get property 'dayOfWeek' of non-object

代码:

$dailyLog = DailyLog::with(['todoList','user'])->find(\DB::table('daily_logs')->max('id'));
$weekday = $dailyLog->date->dayOfWeek; 
$yesterdaysLog = DailyLog::loadByDate(Carbon::now()->subMonth(1)->next($weekday));

我正在尝试从第一个查询中获取星期几,并将其与上个月的同一天进行比较

这:$dailyLog->date 很可能不是 Carbon 对象,因此你不能调用 dayOfWeek.. 确保 date returns 是 Carbon实例.

要在您的 DailyLog 模型中执行此操作,请添加:

protected $dates = ['date'];

这将确保 date 转换为 Carbon。但还要确保由于某种原因该字段也不是 null