Php Laravel雄辩的渴望加载:两次连接同一个表

Php Laravel雄辩的渴望加载:两次连接同一个表,php,database,join,laravel,eloquent,Php,Database,Join,Laravel,Eloquent,我有一个用户表和一个约会表。在预约表中,我有两个用户ID(客户ID、员工ID)。我想检索所有带有客户姓名和员工姓名的约会 users table id name appointments table id staff_id(user_id) customer_id(user_id) datetime 如您所见,我必须将users表与appoints表连接两次。 通常我使用内部联接来执行此操作 我们可以使用with()对Laravel雄辩的渴望加载做同样的事情吗 我们能做些什么吗 appoin

我有一个用户表和一个约会表。在预约表中,我有两个用户ID(客户ID、员工ID)。我想检索所有带有客户姓名和员工姓名的约会

users table
id
name

appointments table
id
staff_id(user_id)
customer_id(user_id)
datetime
如您所见,我必须将users表与appoints表连接两次。 通常我使用内部联接来执行此操作

我们可以使用with()对Laravel雄辩的渴望加载做同样的事情吗

我们能做些什么吗

appointments::with('users' * )->get();?
* Do something here to inner join users table twice, and read user1.name as staff_name,   user2.name as customer_name.
这是我需要的最终输出:

appointment_id
staff_id
staff_name
customer_id
customer_name
datetime
我还有一个问题,下面查询中的第二个参数是什么

User::with(array(
    'post'=> function() use $region {
          //what is use $region means? Can you give me an example?
     }
));
谢谢

class T1 extends Eloquent {

    protected $table = 't1';

}

class T2 extends Eloquent {

    protected $table = 't2';

    public function customer()
    {
        return $this->belongsTo('T1','c_id');//c_id - customer id
    }
    public function staff()
    {
        return $this->belongsTo('T1','s_id');//s_id - staff id
    }
}
  • 使用“With”:

    $list=\T2::with('customer')->with('staff')->get();
    foreach($列为$行){
    回显'ID:'.$row->ID',客户:'.$row->customer->name',员工:'.$row->staff->name'.
    ; }
  • 连接:

    $list = \T2::leftJoin('t1 as customer_table', 'customer_table.id','=','t2.c_id')
         ->leftJoin('t1 as staff_table', 'staff_table.id','=','t2.s_id')
         ->select('staff_table.name as staff_name','customer_table.name as customer_name')
         ->get();
    foreach ($list as $row) {
        echo 'customer: '.$row->customer_name.', staff: '.$row->staff_name.'<br>';
    }
    
    $list=\T2::leftJoin('t1作为客户_表,'customer_表.id','=','T2.c_表id'))
    ->leftJoin('t1作为staff_表'、'staff_表.id'、'='、't2.s_id')
    ->选择(‘staff_table.name作为staff_name’、‘customer_table.name作为customer_name’)
    ->get();
    foreach($列为$行){
    回显“客户:”.$row->customer_name.”,职员:“.$row->staff_name.”
    ; }
  • 关于第二个问题-这是用于子查询的。查看文档:

    $list = \T2::leftJoin('t1 as customer_table', 'customer_table.id','=','t2.c_id')
         ->leftJoin('t1 as staff_table', 'staff_table.id','=','t2.s_id')
         ->select('staff_table.name as staff_name','customer_table.name as customer_name')
         ->get();
    foreach ($list as $row) {
        echo 'customer: '.$row->customer_name.', staff: '.$row->staff_name.'<br>';
    }