2017-01-26 15 views
1

コントローラ方法:Laravel 5.3選択Pluckの方法

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']) }} 

そして、私のテーブル:モデルMatchから

表(テーブル名:マッチ):

enter image description here

モデル Teamから

表(テーブル名:チーム):

enter image description here

teamsテーブルmatchesと(参照)に接続されている(team_a_idteam_b_idteamsに接続されています)。 viewselect方法はIDテーブルで私を返さ:

enter image description here

私はテーブルteamsないidteam_nameを持っている必要があります。 私は方法むしれ変更する場合:

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

これは、私が以前に選択されたレコードを強調している必要がありますmetohd編集です。

+0

は '$ match2'の内容を出力してみてください。

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

とビュー:私はメソッドを記述します正確に何が得られるか見ることができます。 'dd($ match2);'を使ってください。 – Jerodev

+0

@Jerodevこれは 'dd()'を返しましたhttps://images81.fotosik.pl/317/0dbc76714c0d776fgen.png – michal

答えて

0

[OK]を私はこれを修復します。私は2つのテーブルをidを比較し、それが作業モデルビューで

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

;)

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

この$のMATCH2 =マッチを試してみてください::摘み取る( 'team_a_id'、 'ID') - >のtoArray()

+0

いいえ、 – michal

関連する問題