0
查詢我有以下DB模式:Laravel關係:與多個外鍵
//Pages schema
Schema::create('pages', function($table)
{
$table->increments('id');
$table->softDeletes();
$table->timestamps();
$table->integer('parent_id');
$table->integer('ordination');
$table->unsignedInteger('title_id'); //Refer to strings
$table->unsignedInteger('description_id'); //Refer to strings
$table->unsignedInteger('url_id'); //Refer to strings
//Foreign key
$table->foreign('title_id')->references('id')->on('strings');
$table->foreign('description_id')->references('id')->on('strings');
$table->foreign('url_id')->references('id')->on('strings');
});
//Strings
Schema::create('strings', function($table)
{
$table->increments('id');
$table->softDeletes();
$table->timestamps();
$table->text('en');
$table->text('de');
});
我怎麼能檢索對應的URL字符串頁面對象?
我想有一個頁面對象或數組類似如下:
$page['url']['en'] = 'about'
$page['title']['en']= 'About title'
$page['description']['en']= 'About description'
etc..
我可以檢索從相關網址的頁面對象執行以下雄辯查詢:
$page= Pages::whereHas('url', function($url)
{
$url->where('en', '=', 'About');
})->first();
有了這個雄辯模型:
class Pages extends Eloquent {
protected $table = 'pages';
public function url()
{
return $this->belongsTo('Strings');
}
}
這不會檢索字符串值的標題,描述和d網址,但只有他們的ID。
我該怎麼做?
謝謝您的回答,這個解決方案工作正常! – Spetty 2015-02-06 11:34:05