Laravel关系:多外键查询
Laravel relationship: query with multiple Foreign keys
我有以下数据库模式:
//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 字符串中检索页面 Object?
我会有如下页面 object 或数组:
$page['url']['en'] = 'about'
$page['title']['en']= 'About title'
$page['description']['en']= 'About description'
etc..
我可以从相关 url 执行以下 eloquent 查询检索页面 Object:
$page= Pages::whereHas('url', function( $url )
{
$url->where('en', '=', 'About');
})->first();
有了这个 Eloquent 模型:
class Pages extends Eloquent {
protected $table = 'pages';
public function url()
{
return $this->belongsTo('Strings');
}
}
这不会检索标题、描述和 url 的字符串值,而只会检索它们的 ID。
我该怎么做?
关系看起来正确。您唯一需要做的就是实际加载关系。这里最好的方法是预加载。它将数据库查询减少到最低限度:
$pages = Page::with('url')->get();
$page = Page::with('url')->find(1);
$page = Pages::with('url')->whereHas('url', function($url){
$url->where('en', '=', 'About');
})->first();
要预先加载所有字符串关系,只需将它们添加到 with()
调用:
$pages = Page::with('url', 'title', 'description')->get();
我有以下数据库模式:
//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 字符串中检索页面 Object?
我会有如下页面 object 或数组:
$page['url']['en'] = 'about'
$page['title']['en']= 'About title'
$page['description']['en']= 'About description'
etc..
我可以从相关 url 执行以下 eloquent 查询检索页面 Object:
$page= Pages::whereHas('url', function( $url )
{
$url->where('en', '=', 'About');
})->first();
有了这个 Eloquent 模型:
class Pages extends Eloquent {
protected $table = 'pages';
public function url()
{
return $this->belongsTo('Strings');
}
}
这不会检索标题、描述和 url 的字符串值,而只会检索它们的 ID。
我该怎么做?
关系看起来正确。您唯一需要做的就是实际加载关系。这里最好的方法是预加载。它将数据库查询减少到最低限度:
$pages = Page::with('url')->get();
$page = Page::with('url')->find(1);
$page = Pages::with('url')->whereHas('url', function($url){
$url->where('en', '=', 'About');
})->first();
要预先加载所有字符串关系,只需将它们添加到 with()
调用:
$pages = Page::with('url', 'title', 'description')->get();