如何列出一个嵌套表中Laravel所有项目 [英] How to list out all items in a nested table in Laravel
问题描述
我使用Laravel雄辩ORM和我有麻烦预先加载项的显示。
下面是该方案:
- 在用户遵循博客
- 博客有文章
我有一个名为关系数据库表,该表用于存储用户ID和博客ID显示哪些用户是下面这博客。我对博客描述博客的表,我对帖子的表。的关系表将是我的数据透视表连接用户与博客表一起。现在,我需要从所有用户按照列表中的博客列出所有的帖子。
下面是我的用户模型:
公共职能下列(){
返回$这个 - > has_many_and_belongs_to('博客','关系','USER_ID','blog_id');
}
下面是我的博客模式:
公共职能的追随者(){
返回$这个 - > has_many_and_belongs_to('用户','关系','blog_id','user_id说明);
}
公共职能的职位(){
返回$这个 - >的has_many(邮报);
}
这是我如何试图检索列表中的帖子:
$帖=用户::使用(阵列('以下','following.posts'))
- >找到($用户自> ID)
- >以下()
- >采取($计数)
- >获得();
这code只列出了实际的博客,我需要他们的帖子。
感谢你的帮助,请让我知道如果你需要任何更多的细节。
解决方法:
我稍微修改下面的接受的答案,我决定使用JOIN来降低SQL的数额的话费只需1个呼叫。在这里,它是:
$帖=邮政::加入('博客','posts.blog_id','=','blogs.id)
- >加入('关系','blogs.id','=','relationships.blog_id')
- >选择(职位*'。)
- 化合物其中('relationships.user_id','=',$用户可> id)的
- > ORDER_BY('posts.id,降序)
- >采取($计数)
- >获得();
这是无法实现通过本地雄辩的方法。但是你可以用一点流利的方法加入这些表。例如:
修改此:我已经添加了立即加载到发布
查询。
$ USER =用户::发现(1);
$帖= ::后与('博客')//渴望加载此职位所属的博客
- >加入('博客','blogs.id','=','posts.blog_id)
- >加入('关系','relationships.blog_id','=','blogs.id')
- 化合物其中('relationships.user_id','=',$用户可> id)的
- > ORDER_BY('posts.id,降序)//最新职位第一。
- >极限(10)//获取最后的10个职位
- >获得;('职位*'。)
的foreach($职位为$后){
打印($后>标题);
}
如果您还需要这样的用户是继上显示一个侧边栏,例如所有博客的列表。您可以DYI而不是依赖于雄辩,这应该是更快,更加个性化。例如:
$ USER =用户::使用('下面') - >发现(1);
//这将创建一个字典更快的性能,更长远
$辞典=阵列();
的foreach($用户可与GT;下面为$博客){
$辞典[$ blog-> ID = $博客;
}
//获取从这些博客,他遵循最新的10个职位
//观测:注意这里的array_keys
$帖=邮政:: where_in('blog_id',array_keys($ blog_ids))
- > ORDER_BY('posts.id,降序)
- >极限(10)
- >获得();
//水合物与他们拥有博客的所有帖子。
//这样就避免了加载两次博客,并没有影响
//数据库的记录。这只是一个辅助的看法。
的foreach($职位为$后){
$后>的关系['博客'] = $辞典[$后> blog_id]。
}
在视图:
的foreach($用户可与GT;下面为$博客){
打印($ blog->标题);
}
的foreach($职位为$后){
打印($后>标题'@'$后> blog->标题);
}
I am using Laravel's Eloquent ORM and I'm having trouble eager loading items for display.
Here is the scenario:
- Users follow Blogs
- Blogs have Posts
I have a database table named Relationships, this table is used to store the User ID and the Blog ID to show which User is following which Blog. I have a table for Blogs describing the Blog and I have a table for Posts. The Relationships table would be my pivot table to connect the Users with the Blogs tables together. Now, I need to list out all the posts from all the Blogs the User follows in a list.
Here is my User model:
public function following() {
return $this->has_many_and_belongs_to('Blog', 'relationships', 'user_id', 'blog_id');
}
Here is my Blog model:
public function followers() {
return $this->has_many_and_belongs_to('User', 'relationships', 'blog_id', 'user_id');
}
public function posts() {
return $this->has_many('Post');
}
This is how I am trying to retrieve the posts in a list:
$posts = User::with(array('following', 'following.posts'))
->find($user->id)
->following()
->take($count)
->get();
This code only lists out the actual Blogs, I need their Posts.
Thank you for your help, please let me know if you need any more details.
SOLUTION:
I slightly modified the accepted answer below, I decided to use the JOIN to reduce the amount of SQL calls to simply 1 call. Here it is:
$posts = Post::join('blogs', 'posts.blog_id', '=', 'blogs.id')
->join('relationships', 'blogs.id', '=', 'relationships.blog_id')
->select('posts.*')
->where('relationships.user_id', '=', $user->id)
->order_by('posts.id', 'desc')
->take($count)
->get();
This is not achievable by native Eloquent methods. But you can use a bit of Fluent methods to join those tables. For instance:
Edit here: I've added the eager loading to Post
query.
$user = User::find(1);
$posts = Post::with('blog') // Eager loads the blog this post belongs to
->join('blogs', 'blogs.id', '=', 'posts.blog_id')
->join('relationships', 'relationships.blog_id', '=', 'blogs.id')
->where('relationships.user_id', '=', $user->id)
->order_by('posts.id', 'desc') // Latest post first.
->limit(10) // Gets last 10 posts
->get('posts.*');
foreach ($posts as $post) {
print($post->title);
}
If you also need a list of all blogs that such user is following to show on a sidebar, for instance. You can DYI instead of relying on Eloquent, which should be faster and more customizable. For instance:
$user = User::with('following')->find(1);
// This creates a dictionary for faster performance further ahead
$dictionary = array();
foreach ($user->following as $blog) {
$dictionary[$blog->id] = $blog;
}
// Retrieves latest 10 posts from these blogs that he follows
// Obs: Notice the array_keys here
$posts = Post::where_in('blog_id', array_keys($blog_ids))
->order_by('posts.id', 'desc')
->limit(10)
->get();
// Hydrates all posts with their owning blogs.
// This avoids loading the blogs twice and has no effect
// on database records. It's just a helper for views.
foreach ($posts as $post) {
$post->relationships['blog'] = $dictionary[$post->blog_id];
}
On view:
foreach ($user->following as $blog) {
print($blog->title);
}
foreach ($posts as $post) {
print($post->title . ' @'. $post->blog->title);
}
这篇关于如何列出一个嵌套表中Laravel所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!