我有一个项目和报告表,报告表每天都会保存很多项目报告。现在,我想获取项目的最后报告日期属性我在控制器中使用此代码:
$res = Project::with(['reports' => function($query){
$query->orderBy('updated_at', 'DESC')->first();
}])->get();
return view ('home',compact('res'));
在视图中我有这段代码:
@foreach(Auth::user()->projects as $project)
@if($res->date == Now())
<p>hi</p>
@else
<p>By</p>
@endforeach
但是我得到了这个错误:
Property [date] does not exist on this collection instance
并且返回$ res,我给这个杰森:
{"id":51,"user_id":21,"name":"dff","description":"fff","price":4444,"status":0,"darsad":4,"start_time":"1396-08-18","finish_time":"1396-08-29","created_at":"2017-11-19 08:22:39","updated_at":"2017-11-19 08:22:39","reports":[{"id":20,"user_id":21,"project_id":51,"date":"96\/09\/06","description":"vvvv","created_at":"2017-11-27 11:19:43","updated_at":"2017-11-27 11:19:43"}]}
如何获得任何项目的最新报告?
答案 0 :(得分:0)
假设您的要求只是显示最新的单条记录。可以给出以下方法。
Controller Snippet:
$res = Project::orderBy('updated_at', 'DESC')->get();
return view ('home',compact('res'));
Blade Snippet:
@foreach ($res as $data)
@if ($loop->first)
@if($data->reports[0]->date == Now())
<p>hi</p>
@else
<p>By</p>
@endif
@endif
@endforeach
答案 1 :(得分:0)
Hayy ...您正在尝试比较未定义的值,因为日期是您的响应中的reports数组的一部分。因此,您无法直接从您的回复访问日期。请尝试使用以下代码段而不是您的刀片。它尝试访问报告,然后从报告中访问日期。
<ion-header>
<!-- header html -->
</ion-header>
<ion-content padding>
<form-base [formInputs]="formService.formInputs"></form-base>
</ion-content>