显示另一个表Laravel 8的用户名

发布于 2025-01-22 03:01:29 字数 724 浏览 0 评论 0原文

哈洛,伙计们, 因此,我有2个表

  1. 是列表
  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

  1. is Listings
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸 2025-01-29 03:01:29

您可以将方法一起使用以获取相关用户

public function index(Request $request) {
        $listings = Listing::where('is_active', true)->with(['user', 'tags']) 
            ->orderBy('listings.created_at', 'desc')
            ->get();
}

这样

清单模型


public function user() {
   return $this->belongsTo(User::class);
}

You could just use the with method to get the related user like this

public function index(Request $request) {
        $listings = Listing::where('is_active', true)->with(['user', 'tags']) 
            ->orderBy('listings.created_at', 'desc')
            ->get();
}

but make sure that you add to your Listing model the correct relation like this

Listing Model


public function user() {
   return $this->belongsTo(User::class);
}
年少掌心 2025-01-29 03:01:29

我为控制器推荐此代码:

public function index(Request $request) {
        return $listings = Listing::query()
            ->where('is_active', true)->with(['user', 'tags'])
            ->orderBy('listings.created_at', 'desc')
            ->get();
}

在“模型”部分中,我建议您列出此代码:

public function user() {
    return $this->belongsTo(User::class);
}

‌我建议您填写外国界,关系的所有者keke:如下示例:

public function user()
{
    return $this->belongsTo(User::class, 'user_id', 'id');
}

I recommend this code for controller:

public function index(Request $request) {
        return $listings = Listing::query()
            ->where('is_active', true)->with(['user', 'tags'])
            ->orderBy('listings.created_at', 'desc')
            ->get();
}

In the models section, I suggest you put this code: ‌

public function user() {
    return $this->belongsTo(User::class);
}

I suggest you fill in the fields foreignKey, ownerkey in relation: as in the following example:

public function user()
{
    return $this->belongsTo(User::class, 'user_id', 'id');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文