我想通过用户首先使用查询生成器在“订单”表中输入所选ID来检索user_detail表中的全名列
在订单表中看起来像这样
+"id": 1
+"order_detail_id": 115
+"user_id": 3
+"status": "pending"
+"updated_at": "2019-11-22 17:53:56"
+"created_at": "2019-11-22 15:44:28"
在表user_detail中,它看起来像这样
+"id": 3
+"full_name": michael jackson
+"address": "bla bla bla"
+"updated_at": "2019-11-22 17:53:56"
+"created_at": "2019-11-22 15:44:28"
我想如果用户输入id 1,它将输出类似
"id": 1,
"order_detail_id": 115,
"user_id": 3,
"full_name": "michael jackson",
"address" : "bla bla bla"
"status": "pending"
我尝试使用此代码,但仍然出现错误
$cekclient = DB::table('order')
->where('id',$request->id)
->join('user', 'user.id', '=', 'order.user_id')
->select('order.*','user.name','user.address',)
->get();
dd($cekclient);
答案 0 :(得分:3)
尝试此代码。请给表别名并在选择和联接中使用它们。
$cekclient = DB::table('order as or')
->select('or*','u.name','u.address') // here you can add column names which you need in output
->leftJoin('user as u', 'u.id', '=', 'or.user_id')
->where('or.id',$request->id) // here if you need data by id of user then use u.id in where insead of or.id
->get();