2014-09-04 185 views
1

我有兩個表與此schmea的hasMany關係:獲取與雄辯Laravel

mysql> show columns from table_1; 
+-------------+------------------+------+-----+---------------------+----------- 
| Field  | Type    | Null | Key | Default    | Extra 
+-------------+------------------+------+-----+---------------------+----------- 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_incre 
| id_world | int(11)   | NO |  | NULL    | 
| key   | varchar(255)  | NO |  | NULL    | 
| name  | varchar(255)  | NO |  | NULL    | 
| description | varchar(255)  | NO |  | NULL    | 
| level  | int(11)   | NO |  | 0     | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 | 
+-------------+------------------+------+-----+---------------------+----------- 
8 rows in set (0.00 sec) 

mysql> show columns from table_2; 
+--------------+------------------+------+-----+---------------------+---------- 
------+ 
| Field  | Type    | Null | Key | Default    | Extra 
     | 
+--------------+------------------+------+-----+---------------------+---------- 
| id   | int(10) unsigned | NO | PRI | NULL    | auto_incr 
| key   | varchar(255)  | NO |  | NULL    | 
| level  | int(11)   | NO |  | NULL    | 
| name   | varchar(255)  | NO |  | NULL    | 
| description | varchar(255)  | NO |  | NULL    | 
| price  | int(11)   | NO |  | NULL    | 
| amount  | int(11)   | NO |  | NULL    | 
| created_at | timestamp  | NO |  | 0000-00-00 00:00:00 | 
| updated_at | timestamp  | NO |  | 0000-00-00 00:00:00 | 
+--------------+------------------+------+-----+---------------------+---------- 
30 rows in set (0.00 sec) 

我想所有領域「,從TABLE_2其中table_2.key = table_1.key AND table_2.level = 10「這是我的模型中hasMany選項的正確方法嗎?

我正常的查詢如下所示:

SELECT A.key AS p_key, 
     A.name AS p_key, 
     A.description AS p_desc, 
     A.level AS p_level, 
     B.key AS r_key, 
     B.level AS r_level, 
     B.name AS r_name, 
     B.description AS r_desc 
FROM 
     table_1 AS A, 
     table_2 AS B 
WHERE 
     B.key = A.key AND 
     B.level = '1' 
+0

使用Eloquent關係與非唯一字段作爲鍵可能會導致意外的行爲。你不應該那樣做。 – 2014-09-04 21:45:43

回答

4

要使用這些表來構建hasMany的關係,你需要先創建兩個模型,例如:

class TableOne extends Eloquent { 

    protected $table = 'table_1'; 

    public function tableTwoItems() 
    { 
     return $this->hasMany('TableTwo', 'table_2.key', 'table_1.key') 
        ->where('table_2.level', 1); 
    } 
} 

class TableTwo extends Eloquent { 

    protected $table = 'table_2'; 
} 

一旦你創建了這些模型你的app/models目錄,那麼你可以使用這樣的事情:

$result = TableOne::with('tableTwoItems')->get(); 

要選擇的項目/場,你可以使用這樣的事情:

$result = TableOne::with(array('tableTwoItems' => function($query){ 
    $query->select('table_2.key as k2', 'table_2.name as name2', 'more...'); 
}))->select('table_1.key as k1', 'table_1.name as name1', 'more...')->get(); 

您可以訪問它們喜歡:

$result->first()->tableTwoItems->first(); 

或者你可以循環$result,你也可以循環使用的相關項目嵌套循環。例如:

foreach($result as $tableOneItem) 

    echo $tableOneItem->name; 

    foreach($tableOneItem->tableTwoItems as $tabletwoItem) 

     echo $tabletwoItem->name; 

    endforeach; 

endforeach; 

嘗試在這兩個表的key使用不同的字段名,使他們獨特的爲好。有關更多信息,請閱讀Eloquent Relation文檔。

+0

1您不需要在'hasMany'定義中使用前綴字段'table_1.key'。 2你的第二個例子'with() - > select()'是錯誤的,請記住這些表沒有被加入,所以它會導致'unknown column'錯誤。 3你不需要爲關係使用'PK',在這種情況下,它不是表的'PK',也不是唯一的 - 請參閱我對該問題的評論。 – 2014-09-04 21:49:11

+0

感謝隊友@JarekTkaczyk_deczo_,我已經更新了答案,我不確定我是如何犯這個錯誤的:-) – 2014-09-04 21:56:14

+1

我也沒有,可能你只是需要一杯咖啡或什麼;) – 2014-09-04 22:08:55