Php 当我将数据库数据从laravel 6中的控制器传递到视图时,视图中存在未定义的变量
我是拉雷维尔的新手。我的数据库中有两个表。我希望在“选择”下拉列表中显示数据库中的数据。但当我将数据传递到视图文件时,它给出了一个错误,未定义的变量$groups。请帮帮我。这是我的意见表Php 当我将数据库数据从laravel 6中的控制器传递到视图时,视图中存在未定义的变量,php,mysql,laravel,Php,Mysql,Laravel,我是拉雷维尔的新手。我的数据库中有两个表。我希望在“选择”下拉列表中显示数据库中的数据。但当我将数据传递到视图文件时,它给出了一个错误,未定义的变量$groups。请帮帮我。这是我的意见表 <form method="post" id="frm"> {{csrf_field()}} <div class="panel-body"> <div class="row text-center"> <div class="col">Group
<form method="post" id="frm">
{{csrf_field()}}
<div class="panel-body">
<div class="row text-center">
<div class="col">Group
<select class="selectpicker boundary" data-live-search="true">
@foreach($group as $i)
<option value="{{$i->id}}">{{$i->gtitle}}</option>
</select>
</div>
</div><br>
<div class="row text-center">
<div class="col title">Title
<input type="text" name="title" id="title">
</div>
</div>
</div>
<div class="panel-footer text-center">
<input type="submit" name="submit" id="submit" value="Submit" class="btn btn-info"/>
<input type="reset" name="reset" value="Reset" class="btn btn-secondary"/>
</div>
</form>
这是我的路线
Route::get('/index', 'GroupController@index');
这是我的模型
class Group extends Model{
protected $table = 'groups';
protected $fillable = ['gtitle'];
}
使用compact而不是数组
public function index(){
$group = Group::all();
return View::make("index",compact('group'));
}
这应该行得通,在我的代码上运行得很好
公共功能索引(){
$group=group::all();
返回视图('index')//索引文件的位置
->带('group',$group);
}
@foreach($i组)
{{$i->gtitle}
您的foreach
引用$group
而不是$groups
(注意缺少s
)我已经更改了它,但仍然出现错误。这是因为您对控制器方法做了相同的操作。您雄辩的模型正在填充$groups
变量,但您正在将$groups
发送到您的视图中。请告诉我如何操作。我是一个初学者。请给出一个答案。我的控制器或型号有什么问题吗
public function index(){
$group = Group::all();
return View::make("index",compact('group'));
}
<select class="selectpicker boundary" data-live-search="true">
@foreach($group as $i)
<option value="{{$i->id}}">{{$i->gtitle}}</option>
</select>