php Laravel:如何使用 where 语句中的值填充刀片 SELECT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17913923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: how to populate a blade SELECT with values from a where statement
提问by Josh
I understand you can send values to a select statement like this:
我知道您可以将值发送到这样的选择语句:
Controller:
控制器:
$client = Client::lists('name', 'id');
return View::make('index', compact('client'));
And populate this in my view like so:
并在我看来像这样填充它:
View:
看法:
{{ Form::select('client_id', $client, Input::old('client_id')) }}
But how do I populate only records from Clients where group_id = 1 for example.
但是,例如,如何仅填充来自 group_id = 1 的客户端的记录。
I tried:
我试过:
$client = Client::lists('name', 'id')->where('group_id', 1)->get();
and
和
$client = Client::lists('name', 'id')->where('group_id','=', 1)->get();
But it doesn't seem to work like that and gives me the error "Call to a member function where() on a non-object"
但它似乎不像那样工作,并给我错误“调用非对象上的成员函数 where()”
Any ideas on how to make it work?
关于如何使其工作的任何想法?
采纳答案by Josh
I found an answer that worked for me:
我找到了一个对我有用的答案:
Use fluent instead of eloquent, which will look something like this:
使用 fluent 而不是 eloquent,它看起来像这样:
$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, thanks for trying to help.
@KyleK,感谢您的帮助。
回答by Adrian Ortiz Martinez
Controller:
控制器:
$client = Client::where('group_id', 1)->pluck('name', 'id');
View:
看法:
{!! Form::select('client_id', $client, Input::old('client_id'), ['class'=> 'form-control']) !!}
Result:
结果:
<select id="client_id" class="form-control" name="client_id">
<option value="1">John</option>
<option value="2">Karen</option>
</select>
回答by Loken Makwana
The lists() must be called at last
list() 必须最后被调用
$client = Client::where('group_id','=', 1)->lists('name','id');
回答by Abbas
This would work too
这也行
Client::where('group_id','=', 1)->lists('name');
回答by Kylie
Not sure if this is a typo or not, but you're not retrieiving properly,
不确定这是否是一个错字,但您没有正确检索,
This line should be...
这条线应该...
$client = Client::lists('name', 'id')->where('group_id','=', 1)->get();
Also....
还....
Sometimes when populating lists, you will get models, and its easy to pass them, but sometimes u get arrays (fluent, raw etc) , and in those cases you have to access manually, and build the form with HTML because you have to access it differently.
有时在填充列表时,您会得到模型,并且很容易传递它们,但有时您会得到数组(流畅、原始等),在这些情况下,您必须手动访问,并使用 HTML 构建表单,因为您必须访问它不同。
回答by Mateusz
If you prefer to use Laravel's query builder (e.g. for performance reasons):
如果您更喜欢使用 Laravel 的查询构建器(例如出于性能原因):
$clients = DB::table('clients')->get()->pluck('name', 'id')->toArray();
return view('index', compact('clients'));
or, with a select
to pull only what's needed:
或者,使用 aselect
只提取需要的内容:
$clients = DB::table('clients')->select('name', 'id')->get()->pluck('name', 'id')->toArray();
return view('index', compact('clients'));
In your view:
在您看来:
{!! Form::label('client_id', 'Client') !!}
{!! Form::select('client_id', $clients, old('client_id')) !!}