首页 > 解决方案 > Laravel 8:雄辩的查询似乎显示从数据库中检索到的数据

问题描述

我正在使用 Laravel 8 开发基本的在线订购系统,目前我有一个名为的表orders,每个订单都可以具有以下状态之一:

suspending, awaiting, verified, confirmed, ignored, 和rejected.

所以在控制器上,我编写了这个代码:

public function index()
    {
        $uid = auth()->user()->id;
        $suspendeds = Order::where('status', 'suspending')->where('user_id', $uid)->latest()->paginate(2);
        $awaitings = Order::where('status', 'awaiting')->where('user_id', $uid)->latest()->paginate(2);
        $verified = Order::where('status', 'verified')->where('user_id', $uid)->latest()->paginate(2);
        $confirmed = Order::where('status', 'confirmed')->where('user_id', $uid)->latest()->paginate(2);
        $canceled = Order::whereIn('status', ['rejected','ignored'])->where('user_id', $uid)->latest()->paginate(2);
        return view('profile.index', compact(['suspendeds','awaitings','verified','confirmed','canceled']));
    }

然后在 Blade 上,我添加了这个:

<div class="profile-body BKoodakBold">
      @include('profile.orders.suspends')
      @include('profile.orders.awaites')
      @include('profile.orders.verifies')
      @include('profile.orders.confirms')
      @include('profile.orders.cancels')
</div>

每个 Blade 都遵循相同的结构,就像这样:

<tbody>
    @foreach($verified as $verify)
    <tr>
        <td><input class="form-control" type="text" value="{{ $verify->title }}" disabled="disabled"></td>
        <td><input class="form-control" type="text" value="{{ $verify->material }}" disabled="disabled"></td>
        <td><input class="form-control" type="text" value="{{ $verify->color }}" disabled="disabled"></td>
        <td><input class="form-control"type="text" value="{{ $verify->description }}" disabled="disabled"></td>
    </tr>
    @endforeach
</tbody>

但现在的问题是,它根本没有显示任何东西!orders但是,在我之前写的那些特定状态之后,桌上有许多可用的订单。

而且,如果我dd()在 Controller 方法中使用每个变量,它将在页面上成功打印数据,这意味着查询工作正常。

那么这里出了什么问题呢?如何正确显示数据?

标签: phplaraveleloquentlaravel-8

解决方案


您只需要为子视图提供变量

  @include('profile.orders.suspends', ['suspends' => $suspends])
  @include('profile.orders.awaites', ['awaites' => $awaites])
  @include('profile.orders.verifies', ['verifies' => $verifies])
  @include('profile.orders.confirms', ['confirms' => $confirms])
  @include('profile.orders.cancels', ['cancels' => $cancels])

推荐阅读