下拉列表正在填充 laravel 6 中的空列表

Laravel 版本 6。PHP 7.4。


我只是想从数据库表中填充我的下拉值。最初,它返回错误“变量未定义”,但是当我将代码包含在if条件中时,我的错误消失了,但下拉列表为空。


请建议我卡在哪里。


路线.php


Route::get('products/qrcodes/basic','niceActionController@getMake');

控制器


<?php


namespace App\Http\Controllers;

use \Illuminate\Http\Request;

use App\NiceAction;

use App\NiceActionLogged;


public function getMake()

{

$records = DB::table('users')->get();

return view('products.qrcodes.basic', ['records' => $records]);

}

看法


<form>

   <select  required>

     @if ((empty($records)))

        Whoops! Something went wrong

     @else

     @foreach ($records as $item)

     <option value="{{ $item->id }}">{{ $item->name }}</option>

     @endforeach

     @endif

   </select>

</form>

我也试过“dd($records);” 但没有任何反应。


肥皂起泡泡
浏览 80回答 2
2回答

芜湖不芜

首先确保你的表有数据。然后更改您的控制器并像这样查看。控制器代码<?php&nbsp; &nbsp;namespace App\Http\Controllers;&nbsp; &nbsp;use DB;&nbsp; &nbsp;use \Illuminate\Http\Request;&nbsp; &nbsp;use App\NiceAction;&nbsp; &nbsp;use App\NiceActionLogged;&nbsp; &nbsp;public function getMake() {&nbsp; &nbsp; &nbsp; &nbsp;$records = DB::table('users')->get()->toArray();&nbsp; &nbsp; &nbsp; &nbsp;return view('products.qrcodes.basic', compact('records'));&nbsp; &nbsp;}查看代码:<form>&nbsp; &nbsp; <select required>&nbsp; &nbsp; &nbsp; &nbsp; @if(empty($records))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Whoops! Something went wrong&nbsp; &nbsp; &nbsp; &nbsp; @else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach ($records as $key => $item)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="{{ $item['id'] }}">{{ $item['name'] }}</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; </select></form>我希望它能帮助你 :)

慕森王

确定的是我错误地定义了两条相同的路线。将我的最新路线粘贴到上一条路线上方后,我的代码就可以工作了。上一条路线:    Route::get('products/qrcodes/{firstQR}最新路线:    Route::get('products/qrcodes/basic','niceActionController@getMake');
打开App,查看更多内容
随时随地看视频慕课网APP