如何使用加入 laravel 从另一个集合中检索用户名

how can i retrieve username from another collection by using join in laravel

//viewing tickets generated by them
        $user= Auth::user()->_id;
        $tickets = ticket::where(function ($query) use ($user) {
            $query->where('user', '=', $user);
        })->get();


  //  created ticket by which user
    $atickets = ticket::where(function ($query) use ($user) {
        $query->where('user_assigned', '=', $user);
    })->get();


    return view('ticketviews', compact('tickets','atickets'));

$tickets returns 记录详细信息,例如 user_id 和票证详细信息,现在我需要显示存储在另一个集合 mongodb 中的特定记录的用户名。谁能建议我这样做的代码。

在您的模型中创建关系 将此插入您的机票模型

public function user(){ return $this->hasOne('PATH_TO_USER_MODEL', 'foreign_key', 'local_key'); }

之后您可以访问用户数据,例如:

$tickets = ticket::where('user_assigned', Auth::user()->_id)->with('user')->get(); 在您可以像 $tickets->user->name

一样访问您的用户数据之后

@ATIKON,它的工作我将代码更改为

$user= Auth::user()->_id; $tickets = ticket::where('user', '=', $user)->get();

    foreach($tickets as $t){
        $use = $t->user;
        $us = User::where('_id', '=', $use)->get();
        foreach($us as $a)
        {
            $username=$a->name;
            $t['name']=$username;
        }
    }
    foreach($tickets as $t) {
        $use1 = $t->user_assigned;
        $us1 = User::where('_id', '=', $use1)->get();
        foreach ($us1 as $a1) {
            $username1 = $a1->name;
            $t['namea'] = $username1;
        }
    }  

非常感谢您的帮助