尝试访问bool类型值的数组偏移量(Laravel)

8xiog9wr  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(190)

我正在学习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

zpgglvta

zpgglvta1#

::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>

相关问题