2016-03-20 24 views
0

我應該有什麼樣的關係從Property模型得到:從表加入到表中洋洋灑灑

  • default_characteristics
  • valuename場從characteristic_value

哪有我這樣做?我可以用一個關係嗎?

在添加characteristic_value表之前,在value_id from property_characteristics之前,我只有字符串value字段,並且全部都在使用。

public function characteristics() { 
    return $this->belongsToMany(
     'Proactiv\DefaultCharacteristic', 
     'property_characteristics', 
     'property_id', 
     'characteristic_id') 
     ->withPivot('value'); 
} // In Property model. 

這是原始SQL返回正確的數據:

SELECT a.name, c.name, d.value FROM properties a 

INNER JOIN property_characteristics b 

on a.id = b.property_id 

INNER JOIN default_characteristics C 

on b.characteristic_id = c.id 

INNER JOIN characteristic_values d 

on b.value_id = d.id 

WHERE a.id = 1 

AND c.name = 'rooms' 

返回:屬性名稱,房間,3

enter image description here

+0

你想構建查詢嗎?或者你的意思是「用一種關係做這件事」? –

+0

我想用Laravel雄辯關係來實現。 –

+0

我認爲您必須在PropertyCharacteristic Model中使用特定值模型創建另一個關係: $ this-> belongsTo(Proactiv \ CharacteristicValue','value_id'); –

回答

0

我認爲最好的選擇,你使用Query Builder爲 或者您可以試試這個:

$property = new Property(); 
$property->with("DefaultCharacteristic.PropertyCharacteristic.CharacteristicValue")->where(...)->first();