Laravel 5.3 选择 pluck 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41870460/
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 5.3 select pluck method
提问by michal
Controller method:
控制器方法:
public function edit($id){
$match2 = Match::pluck('team_a_id', 'id');
return view('admin.accept.edit', compact('match2'));
}
And view file:
并查看文件:
{{ Form::select('matches_id', $match2, null, ['class' => 'form-control']) }}
And my table:
还有我的桌子:
Table from model Match(table name: matches):
模型中的Match表(表名:匹配):
Table from model Team(table name: teams):
模型中的Team表(表名称:团队):
Table teamsis connected (references) with table matches( team_a_idand team_b_idis connected with table teams). The selectmethod with viewreturned me only IDwith tables:
表teams与表相连(引用)matches(team_a_id并team_b_id与表相连teams)。该select用的方法view只返回了我ID的桌子:
I need have team_namewith table teamsnot id.
When I change method pluck:
我需要有team_name与表teams不id。当我改变方法采摘时:
$match2 = Match::pluck('id', 'id');
And view:
并查看:
{{ Form::select('matches_id', Team::find($match2)->team_a_id." vs. ".Team::find($match2)->team_b_id, null, ['class' => 'form-control']) }}
Laravel returned error:
Laravel 返回错误:
Invalid argument supplied for foreach() (View: C:\xampp\htdocs\football\football\resources\views\admin\accept\edit.blade.php)
为 foreach() 提供的参数无效(视图:C:\xampp\htdocs\football\football\resources\views\admin\accept\edit.blade.php)
This is metohd edit so I must have highlighted record that was previously selected.
这是方法编辑,所以我必须突出显示以前选择的记录。
采纳答案by michal
Ok I repair this. I write method:
好的,我修理这个。我写方法:
public function edit($id){
$match = Match::select()->orderBy('updated_at', 'asc')->get();
$selectedMatch = DB::table('usermatches')->find($id)->matches_id;
return view('admin.accept.edit', compact('match', 'selectedMatch'));
}
And view:
并查看:
<select name="matches_id" id="matches_id" class="form-control selectpicker" data-live-search="true" data-size="5">
<option value=0>Wybierz wydarzenie</option>
@foreach($match as $role)
<option value="{{ $role->id }}" {{ $selectedMatch == $role->id ? 'selected="selected"' : '' }}>
{{ Team::find($role->team_a_id)->team_name }} vs. {{ Team::find($role->team_b_id)->team_name }} ({{$role->date}}; <?php echo date('G:i',strtotime($role->time)); ?> | Liga {{ League::find($role->league_id)->name }} | {{ Sport::find($role->sport_id)->name }})
</option>
@endforeach
</select>
In model view I compare idwith two tables and it working ;)
在模型视图中,我id与两个表进行了比较并且可以正常工作;)
{{ $selectedMatch == $role->id ? 'selected="selected"' : '' }}
回答by Luthfi Fs
Try this $match2 = Match::pluck('team_a_id', 'id')->toArray()
试试这个 $match2 = Match::pluck('team_a_id', 'id')->toArray()


