2017-09-25 59 views
0

我即將完成我的CMS,但我有一個小問題。Laravel |保存並顯示兩個關係

我可以創建幾個團隊,工作完美。

我可以製作幾款遊戲,作品也很完美。

現在我想創建這些團隊之間的匹配,這意味着我有兩個數據透視表。 一個叫做game_match,另一個叫match_team

game_match包括game_idmatch_id

match_team包括match_idteam1_idteam2_id

match/create.blade.php對於每個隊兩個下拉領域。

保存與數據庫的單個關係對我來說工作正常,因爲我已經做了幾次,但我無法弄清楚如何保存兩個關係。

這是我走到這一步:

match/create.blade.php

<div class="field m-t-20 is-inline-block"> 
    <p class="control"> 
     <label for="home" class="label"><b>{{ trans_choice('messages.home', 1) }}</b></label> 
     <input type="hidden" name="home" id="home" :value="homeSelected"> 
     <div class="select"> 
      <select v-model="homeSelected"> 
       @foreach($teams as $team) 
        <option value="{{ $team->id }}">{{ $team->name }}</option> 
       @endforeach 
      </select> 
     </div> 
    </p> 
</div> 

<div class="field m-t-20 is-inline-block"> 
    <p class="control"> 
     <label for="opponent" class="label"><b>{{ trans_choice('messages.opponent', 1) }}</b></label> 
     <input type="hidden" name="opponent" id="opponent" :value="opponentSelected"> 
     <div class="select"> 
      <select v-model="opponentSelected"> 
       @foreach($teams as $team) 
        <option value="{{ $team->id }}">{{ $team->name }}</option> 
       @endforeach 
      </select> 
     </div> 
    </p> 
</div> 

@section('scripts') 
    <script> 
     var app = new Vue({ 
      el: '#app', 
      data: { 
       homeSelected: "", 
       opponentSelected: "", 
       gameSelected: "" 
      } 
     }); 
    </script> 
@endsection 

MatchController.php

public function store(Request $request) 
    { 
     $this->validate($request, [ 
      'title' => 'required|max:255', 
      'matchday' => 'required', 
     ]); 

     $match = new Match(); 

     $match->title = $request->title; 
     $match->matchday = $request->matchday; 

     if ($match->save()) { 
      $match->games()->sync($request->game); 
      $match->teams()->sync([ 
       ['team1_id' => $request->home, 'team2_id' => $request->opponent], 
      ]); 

      Session::flash('success', trans('messages.created', ['item' => $match->title])); 
      return redirect()->route('matches.show', $match->id); 
     } else { 
      Session::flash('error', trans('messages.error')); 
      return redirect()->route('matches.create')->withInput(); 
     } 
    } 

match.php

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Match extends Model 
{ 
    use SoftDeletes; // <-- Use This Instead Of SoftDeletingTrait 

    protected $fillable = [ 
     'title' 
    ]; 

    protected $dates = ['deleted_at']; 

    public function setHomeTeam() {} 

    public function teams() { 
     return $this->belongsToMany('App\Team', 'match_team', 'match_id', 'team1_id'); 
    } 

    public function games() { 
     return $this->belongsToMany('App\Game', 'game_match'); 
    } 

    public function getHomeTeam() { 
     return $this->belongsToMany('App\Team', 'match_team', 'match_id', 'team1_id'); 
    } 

    public function getOpponentTeam() { 
     return $this->belongsToMany('App\Team', 'match_team', 'match_id', 'team2_id'); 
    } 
} 

有人可以幫我嗎?

回答

0

您應該更好地使用firstOrCreate(),updateOrCreate或attach()方法。

+0

這並不能解決我在保存同一外表的兩個關係時遇到的問題。 – Kazuto