Laravel - Eloquent:使用用户帖子表从用户那里获取帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38998829/
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: get posts from user with users post table
提问by Luca Garbin
I searched a lot but could not find any answer about it, so I hope u could help me!
我搜索了很多,但找不到任何答案,所以我希望你能帮助我!
I have three tables: posts, users and post_users. Post_users contains two columns: user_id and post_id.
我有三个表:posts、users 和 post_users。Post_users 包含两列:user_id 和 post_id。
I need to get all posts by an user with Eloquent. Any ideas?
我需要使用 Eloquent 获取用户的所有帖子。有任何想法吗?
Thank you so much!
非常感谢!
(I'm sorry if already exists that question, but I didn't found it yet)
(如果已经存在该问题,我很抱歉,但我还没有找到它)
回答by mydo47
Model User
:
型号User
:
public function posts()
{
return $this->belongsToMany('App\Post', 'post_users', 'user_id', 'post_id');
}
Model Post
:
型号Post
:
public function users()
{
return $this->belongsToMany('App\User', 'post_users', 'post_id', 'user_id');
}
Try:
尝试:
$user = User::find($id);
$allpost = $user->posts()->get();
var_dump($allpost);
回答by Sanzeeb Aryal
If you donot have relationship built, try this. However building relationships is preferred. PostUsers and Posts are assumed models of post-users and posts tables, and post as column of the posts table. this gives the posts of logged in user, but not tested.
如果你没有建立关系,试试这个。然而,建立关系是首选。PostUsers 和 Posts 是 post-users 和 posts 表的假定模型,并且 post 作为 posts 表的列。这给出了登录用户的帖子,但未经测试。
$postusers=PostUsers::where(['user_id'=>Auth::user()->id])->get();
foreach($postusers as $postuser)
{
$posts=Posts::where(['post_id'=>$postuser->post_id)]->get();
foreach($posts as $post)
{
print_r($post->post);
}
}
回答by Wild Beard
The Laravel docs cover this pretty well but I had an issue with it when I first started as well. Check out Laravel Relationshipsfor the fully write up.
Laravel 文档很好地涵盖了这一点,但我刚开始时也遇到了问题。查看Laravel 关系以获得完整的文章。
// Inside your Post model
public function user() {
return $this->belongsTo('App\User');
}
// Inside your User model
public function posts() {
return $this->hasMany('App\Posts');
}
You can then create a query:
然后您可以创建一个查询:
$posts = User::find(1)->posts;
Or you can filter it further..
或者你可以进一步过滤它..
$posts = User::find(1)->posts()->where('active', 1)->get();
You may need to change your paths for the hasMany()
and belongsTo()
if your model location and names are different.
您可能需要更改你的路hasMany()
和belongsTo()
如果你的模型的位置和名称不同。
回答by KmasterYC
As you wish. Relation between User and Post is many-to-many
如你所愿。User 和 Post 的关系是多对多的
- Defined relation
- 定义的关系
// In User model
public function posts(){
return $this->belongsToMany(Post::class);
}
// In Post model
public function users(){
return $this->belongsToMany(User::class);
}
- Table structure
- 表结构
- users
id
- posts
id
- post_user
user_id
post_id
- Get all post by a user
- 获取用户的所有帖子
$posts = User::find($user_id)->posts
回答by Parithiban
Try this
尝试这个
From Controller :
从控制器:
dd(Auth::user()->posts);
App/User
应用程序/用户
public function posts()
{
return $this->belongsToMany('App\Entities\posts', 'post_users', 'user_id', 'post_id');
}