我需要根據父母的約束獲得一行的id。我想用雄辯的方式做到這一點,並保持優雅。這個過程開始時需要注意的一些事項: 我有 - country_code(2位iso),lang_code(語言的2位數字縮寫) 我需要 - country_id,lang_id(主鍵) 所以我可以得到--market_id查詢)Laravel 4雄辯 - 根據父母的約束獲取自我的id
我能找回我需要用下面的數據,比較遺憾的是變量的命名(客戶端有奇怪的名字):
// Only receive desired inputs
$input_get = Input::only('marketCode','langCode');
// Need the country based on the "marketCode"
$countryId = Country::where('code',$input_get['marketCode'])->pluck('id');
// Get the lang_id from "langCode"
$languageId = Language::where('lang_abbr',$input_get['langCode'])->pluck('lang_id');
// Get the market_id from country_id and lang_id
$marketId = Market::where('country_id', $countryId)
->where('lang_id',$languageId)->pluck('market_id');
// Get All Market Translations for this market
$marketTranslation = MarketTranslation::where('market_id',$marketId)->lists('ml_val','ml_key');
我試過以下,但這隻急於根據約束加載國家和語言。只有當market_id已知時,Eager Loading似乎纔有用。
class Market extends Eloquent {
protected $primaryKey = 'market_id';
public function country() {
return $this->belongsTo('Country');
}
public function language(){
return $this->belongsTo('Language','lang_id');
}
}
$markets = Market::with(array(
'country' => function($query){
$query->where('code','EE');
},
'language'=> function($query){
$query->where('lang_abbr','et');
}
))->get();
謝謝您的答覆。我探討了這個選項,但真的不想做手動連接或查詢。我真的想要依靠關係和ORM。我發佈了我想出的解決方案。 – 2013-05-01 23:57:34