php - Blade view not fetching data from controller in laravel

In my controller, I'm getting some values from the database and sending them to Blade View. I checked my query through php artisan tinker, and it returns a valid record. The issue is that my Blade view is not fetching values passed from the controller.

Controller

public function getMake()
{
    $records = DB::table('users')->get()->toArray();

    return view('products.qrcodes.basic',compact('records'));
}

Route

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

View

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

Answer

Solution:

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>

Answer

Solution:

try this, it should work :

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

Blade :

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

Answer

Solution:

The issue was with my route file. In route, I had defined another route which was calling the same Blade View I was working on.

So maybe it was a conflict there. I simply removed that route and It worked!

Answer

Solution:

No need to convert collection to array, you can work with your data right after ->get(), like this: DB::table('users')->get();

Source