刀片视图未从Laravel中的控制器获取数据

时间:2020-06-08 10:34:42

标签: php laravel laravel-6

在Controller中,我从数据库中获取了一些值并将其发送到Blade View。 我通过 php artisan tinker 检查了我的查询,并返回了有效记录。

问题是我的Blade视图未获取从控制器传递的值。

控制器

public function getMake()
{
    $records = DB::table('users')->get()->toArray();
    return view('products.qrcodes.basic',compact('records'));
}

路线

Route::get('/basicfile','niceActionController@getMake');

查看

<select>
  <option selected disabled>Make*</option>
   @if(empty($records))
      Whoops! Something went wrong
   @else
   @foreach ($records as $key => $item)
      <option value="{{ $item->id }}">{{ $item->name }}</option>
   @endforeach
   @endif
</select>

4 个答案:

答案 0 :(得分:1)

尝试一下,它应该可以工作:

public function getMake()
{
  $records = DB::table('users')->get();
  return view('products.qrcodes.basic',compact('records'));
}

刀片:

<select>
  <option selected>Make*</option>
   @if(empty($records))
     <p>No records Found</p>
   @else
   @foreach ($records as $item)
      <option value="{{ $item->id }}">{{ $item->name }}</option>
   @endforeach
   @endif
</select>

答案 1 :(得分:1)

niceActionController

public function getMake()
{
  $records = DB::table('users')->get();
  return view('products.qrcodes.basic',compact('records'));
}

basic.blade.php

<select>
  <option selected>Make*</option>
   @if(empty($records))
     <p>No records Found</p>
   @else
   @foreach ($records as $item)
      <option value="{{ $item->id }}">{{ $item->name }}</option>
   @endforeach
   @endif
</select>

答案 2 :(得分:1)

感谢所有花费宝贵时间在上面给出答案的人。 我自己发现了这个问题。

问题出在我的路线文件上。 在路线上,我定义了另一条路线,该路线正在使用相同的 Blade View 。 可能是那里发生了冲突。我简单地删除了那条路线,它起作用了! 哇!!!!

答案 3 :(得分:0)

无需将集合转换为数组,可以在->get()之后立即使用数据,如下所示: DB::table('users')->get();