Laravel 错误异常:未定义变量:类别
我对Laravel7还很陌生,我一直在学习框架的同时尝试创建一个系统。我被困在未定义的变量错误上,创建了一个名为create inside device folder的表单,它有几个字段。我想从名为equipcat的db表中提取数据,并将其显示在select标记上。如果使用{{form}方法,它可以工作,但是下面的方法失败,我更喜欢使用下面的方法,而不是{form} create.blade.phpLaravel 错误异常:未定义变量:类别,laravel,Laravel,我对Laravel7还很陌生,我一直在学习框架的同时尝试创建一个系统。我被困在未定义的变量错误上,创建了一个名为create inside device folder的表单,它有几个字段。我想从名为equipcat的db表中提取数据,并将其显示在select标记上。如果使用{{form}方法,它可以工作,但是下面的方法失败,我更喜欢使用下面的方法,而不是{form} create.blade.php <select class="form-control input-sm&qu
<select class="form-control input-sm" id="category_name" name="category_name">
<option value="">--Choose Category--</option>
@foreach ($categories as $row)
<option>{{$row->name}}</option>
@endforeach
</select>
Web.app
Route::get('equipment.create', 'CategoryController@getcategory');
您可以使用“选择”而不是“弹拨”
试试这个,希望它能有所帮助。最好的方法是使用模型进行查询。将您的CategoryController更改为:
$categories = Category::select("name","cat_id")->get();
return view('equipment.create',compact('categories'));
我希望你有分类模型。我找到了一个解决方案。在我的create.blade.php中,我在声明的变量中添加了@php($categories=\App\Category::all())这一行,它就工作了。我的create.blade.php如下所示:
I found a solution. In my create.blade.php I added this line @php($categories=\App\Category::all()) to declared the variable and it worked. my create.blade.php looks like this:
@php($categories=\App\Category::all())
@foreache($categories as $row)
<option value="">{{ $row->name }}</option>
@php($categories=\App\Category::all())
@前额疼痛($类别为$行)
{{$row->name}
欢迎来到SO。。。我想你应该把屏幕截图作为你的新作也许有另一个动作返回这个视图,它没有通过类别
?我已经上传了我的屏幕截图@Kamlesh PaulYes,我会给它一个截图。非常感谢。
$categories = Category::select("name","cat_id")->get();
return view('equipment.create',compact('categories'));
I found a solution. In my create.blade.php I added this line @php($categories=\App\Category::all()) to declared the variable and it worked. my create.blade.php looks like this:
@php($categories=\App\Category::all())
@foreache($categories as $row)
<option value="">{{ $row->name }}</option>