Laravel 5.3 select 采摘法

Laravel 5.3 select pluck method

控制器方法:

public function edit($id){

    $match2 = Match::pluck('team_a_id', 'id');
    return view('admin.accept.edit', compact('match2'));

}

并查看文件:

{{ Form::select('matches_id', $match2, null, ['class' => 'form-control']) }}

还有我的table:

Table 来自模型 Match(table 名称:匹配):

Table 来自模型 Team(table 名称:团队):

Table teams 与 table matches( team_a_idteam_b_id 与 [=62= 相关] teams)。带有 viewselect 方法仅返回 ID 带有 tables:

我需要 team_name 和 table teams 而不是 id。 当我改变方法 pluck:

$match2 = Match::pluck('id', 'id');

并查看:

 {{ Form::select('matches_id', Team::find($match2)->team_a_id." vs. ".Team::find($match2)->team_b_id, null, ['class' => 'form-control']) }}

Laravel 返回错误:

Invalid argument supplied for foreach() (View: C:\xampp\htdocs\football\football\resources\views\admin\accept\edit.blade.php)

这是方法编辑,所以我必须突出显示之前选择的记录。

好的,我修复这个。我写的方法:

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'));

}

并查看:

  <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>

在模型视图中,我将 id 与两个表进行了比较并且它有效 ;)

{{ $selectedMatch == $role->id ? 'selected="selected"' : '' }}