【发布时间】:2021-02-28 20:56:38
【问题描述】:
我有 3 张桌子。1) Recent Views 2) posts 和 3) users。我需要用中间关系加入这三个表。
recent table
------------
id
post_id
user_id
date
posts table
-----------
id
user_id
post_title
description
date
users table
-----------
id
username
image
email
date
现在需要获取来自recent table 的所有数据,其中user_id = logged_users_id -> 将posts table 与recent.post_id = posts.id 连接在一起-> 将“用户表”与posts.user_id = users.id 连接起来。我怎么能用 laravel eloquent 做到这一点?有人可以帮我解决这个问题吗?
我使用的代码是:
$contents = RecentView::where('user_id', $loggedUser)
->with('posts')
->with('user')
->paginate(12)
->toArray();
但它使用recent.user_id = users.id 加入用户表
【问题讨论】:
-
你写的代码现在可以用了吗?还是您正在寻找更好的解决方案?
-
没有。我得到的结果是错误的。从用户表中获取的是每个帖子的记录用户详细信息,而不是帖子所有者详细信息
-
如果我像 laravel 的新手一样做错任何事情,也可以提供更好的解决方案建议
-
测试我发送的样本
-
试过了。但出现错误
标签: laravel eloquent laravel-query-builder eloquent-relationship