如何在laravel 5.4中进行左连接

问题描述 投票:0回答:1

我有两个表1)客户2)订单。我试图制作leftjoin,但我得不到我想要的正确结果。请看下面的图片。

谢谢

ClientController.php

$clients = Client::leftjoin('orders','orders.client_id','=','clients.id')
                    ->select(DB::raw("count(orders.id) as orderCount"),
                    'clients.name as name','clients.email as email',
                    'clients.phone as phone','clients.country as country',
                    'orders.total_amount')
                    ->where('orders.status',0)
                    ->get();

enter image description here

mysql join laravel-5 left-join right-to-left
1个回答
0
投票
   $myevents = DB::table('users')
        ->join('event_model_user', 'users.id', '=', 'event_model_user.user_id')
        ->rightJoin('events', 'event_model_user.event_model_id', '=', 'events.id')
        ->where('users.id','=',$user->id)
        ->orWhere('events.bookable','=',$user->zipcode)
        ->get();

这是我使用正确的联接来获取没有用户做过任何预订的事件的示例!

在你的情况下,这将是(未经测试)

 $myevents = DB::table('clients')
        ->leftJoin('orders','orders.client_id','=','clients.id')
        ->where('orders.status',0)
        ->get();
© www.soinside.com 2019 - 2024. All rights reserved.