USER table, fields id, name
ACCOUNT table, fields, id,name,account
ORDER table, fields, id,uid,aid,content
How to use related query in laravel with 3 tables
Query the detailed information of the order in the order table through the user id in the user table, and then query the account information of the account account through the aid in the order
漂亮男人2017-05-16 16:49:53
//在模型中设置关联
class Order extends Model
{
public function user()
{
return $this->belongsTo('App\User', 'uid', 'id');
}
}
//查询出id为1的用户所有的订单,并用with关联出user的信息
$orders= Order::whereHas('user', function ($q) {
$q->where('id', 1);
})->with('user')->get();
Result