我正在学习Laravel,我试图显示一个项目的详细信息,但我得到这个ErrorException
我的控制器
public function show($request)
{
$request = DriverRequest::find($request);
// dd($request);
return view ('driverDashboard.showRequestDetails',['request' => $request]);
// return view ('driverDashboard.showRequestDetails',['requests' => $request]);
}
我foreach
@foreach ($request as $request)
<div class="body txt-c d-flex p-20 mt-20 mb-20 block-mobile">
<div>
{{ $request ['name'] }}</td>
</div>
<div>
{{ $request ['budget'] }}</td>
</div>
<div>
{{ $request ['status'] }}</td>
</div>
</div>
@endforeach
这里是我的ErrorException
::find()
只返回单个DriverRequest
对象或null
。根本不需要foreach()
;只做$request->name
,$request->budget
等
另外,$request
是一个不好的变量名;应该只用于Laravel中的全局Request
对象,如public function example(Request $request)
或类似的。
修改代码如下:
public function show($id) {
$driverRequest = DriverRequest::findOrFail($id);
return view ('driverDashboard.showRequestDetails', ['driverRequest' => $driverRequest]);
}
然后在你的showRequestDetails.blade.php
:
<div class="body txt-c d-flex p-20 mt-20 mb-20 block-mobile">
<div>
{{ $driverRequest->name }}</td>
</div>
<div>
{{ $driverRequest->budget }}</td>
</div>
<div>
{{ $driverRequest->status }}</td>
</div>
</div>