Laravel:如何使用where语句中的值填充刀片SELECT [英] Laravel: how to populate a blade SELECT with values from a where statement
问题描述
我知道您可以像这样将值发送到select语句:
I understand you can send values to a select statement like this:
控制器:
$client = Client::lists('name', 'id');
return View::make('index', compact('client'));
在我的视图中这样填充:
And populate this in my view like so:
查看:
{{ Form::select('client_id', $client, Input::old('client_id')) }}
但是我如何只填充例如group_id = 1的客户端记录.
But how do I populate only records from Clients where group_id = 1 for example.
我尝试过:
$client = Client::lists('name', 'id')->where('group_id', 1)->get();
和
$client = Client::lists('name', 'id')->where('group_id','=', 1)->get();
但是它似乎无法正常工作,并给我错误在非对象上调用成员函数where()"
But it doesn't seem to work like that and gives me the error "Call to a member function where() on a non-object"
关于如何使其工作的任何想法?
Any ideas on how to make it work?
推荐答案
我找到了一个对我有用的答案:
I found an answer that worked for me:
使用流利而不是雄辩,这看起来像这样:
Use fluent instead of eloquent, which will look something like this:
$client = DB::table('clients')->where('group_id', 1)->lists('name');
return View::make('index', compact('client'));
然后在您的视图中只需在刀片形式标签中调用它,就像这样:
Then in your view just call it inside blade form tags like this:
{{ Form::select('client_id', $client, Input::old('client_id')) }}
@KyleK,感谢您的帮助.
@KyleK, thanks for trying to help.
这篇关于Laravel:如何使用where语句中的值填充刀片SELECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!