显示另一个表Laravel 8的用户名
哈洛,伙计们, 因此,我有2个表
- 是列表
- 是列表中的用户
,我有一些列,一个是User_id,并且与用户表相关。 我想显示与用户表相关的用户名。 在索引刀片中,我使用一些标签。 但是,当我使用- > rightjoin(“用户”,“ users.id”,“ =”,“ listings.user_id”)
它的工作 加入打破了我的标签使它们默认,与其他帖子相同。
public function index(Request $request)
{
$listings = Listing::where('is_active', true)->with('tags')
//->rightjoin("users", "users.id", "=", "listings.user_id") //don't show tags of the posts
->orderBy('listings.created_at', 'desc')
->get();
//check if posts ar listing by last date or something
$tags = Tag::orderBy('name') // variable displayed in view
->get();
Hellow guys,
so I have 2 tables
- is Listings
- is Users
in Listings I have some columns, one is user_id, and is related to users tables.
I want to display the user name related to the user table.
in the index blade, I use some tags.
But when I use ->rightjoin("users", "users.id", "=", "listings.user_id")
it's works but,
the join broke my tags make them default, same with others posts.
public function index(Request $request)
{
$listings = Listing::where('is_active', true)->with('tags')
//->rightjoin("users", "users.id", "=", "listings.user_id") //don't show tags of the posts
->orderBy('listings.created_at', 'desc')
->get();
//check if posts ar listing by last date or something
$tags = Tag::orderBy('name') // variable displayed in view
->get();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
与
方法一起使用以获取相关用户
这样
清单模型
You could just use the
with
method to get the relateduser
like thisbut make sure that you add to your
Listing
model the correct relation like thisListing Model
我为控制器推荐此代码:
在“模型”部分中,我建议您列出此代码:
我建议您填写外国界,关系的所有者keke:如下示例:
I recommend this code for controller:
In the models section, I suggest you put this code:
I suggest you fill in the fields foreignKey, ownerkey in relation: as in the following example: