简体   繁体   中英

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>

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>

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>

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM