2016-03-23 90 views
0

所以我有以下match表,其中包含參加比賽的球隊的數量。我想建立與teams它看起來像這樣的關係:我想有像$this->match->where("match", "=", "2")->first()->teams();與多列的雄辯關係

隊表

| id | number | name | etc | 
| 1 | 1234 | Example | etc | 
| 2 | 2345 | Example | etc | 

etc... 

匹配表

| id | match | red1 | red2 | blue1 | blue2 | 
| 1 | 1 | 1234 | 1710 | 673 | 2643 | 
| 2 | 2 | 2345 | 1677 | 4366 | 246 | 

etc... 

我試過使用hasMany(),但我似乎無法使用red1, red2, blue1, blue3列。

我曾嘗試:

class Matches extends Model 
{ 
    protected $table = "match_table"; 

    protected $fillable = [ 
     "match_id", 
     "time", 
     "bluescore", 
     "redscore", 
     "red1", 
     "red2", 
     "red3", 
     "blue1", 
     "blue2", 
     "blue3", 
    ]; 

    public function teams() 
    { 
     return $this->hasMany("App\Models\Teams", "number", ["red1", "red2", "blue1", "blue2"]); 
    } 
} 

回答

0

我最終什麼事做的只是通過我想,然後就返回一個新Collection在它的結果的每一列循環。

public function teams() 
{ 
    $result = []; 

    foreach($this::$teamColumns as $column) { 
     $result[] = $this->hasMany("App\Models\Teams", "number", $column)->first(); 
    } 

    return new Collection($result); 
}