Laravel Eloquent关系子关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32723128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent Relationship Sub-Relationship?
提问by thesunneversets
I have an Offer class with the following relationship:
我有一个具有以下关系的 Offer 类:
class Offer extends Model
{
public function releases()
{
return $this->belongsToMany('App\Release');
}
}
The Release class has this relationship:
Release 类具有以下关系:
class Release extends Model
{
public function artist()
{
return $this->belongsTo('App\Artist');
}
}
What is the simplest way of amending
最简单的修改方法是什么
App\Offer::with('releases')->get();
So as to also get Artist information in each release?
以便在每个版本中也获得艺术家信息?
回答by ArjanSchouten
You can do this by:
您可以通过以下方式执行此操作:
App\Offer::with('releases.artist')->get();
You can look at: http://laravel.com/docs/5.1/eloquent-relationships#querying-relationsfor more information.
您可以查看:http: //laravel.com/docs/5.1/eloquent-relationships#querying-relations了解更多信息。
If you have multiple releations you can query them by doing:
如果您有多个关系,您可以通过执行以下操作来查询它们:
App\Offer::with(['releases.artist', 'releases.albums'])->get();
When you scrolldown a bit you see
当您向下滚动一点时,您会看到
// Retrieve all posts that have at least one comment with votes...
$posts = Post::has('comments.votes')->get();
Remember the "dot" notations is a standard convention of Laravel. For example loading a view:
请记住,“点”符号是 Laravel 的标准约定。例如加载视图:
view('folder.viewfile');
The same applies to eloquents nested relationships.
这同样适用于 eloquents 嵌套关系。