在laravel中将原始查询转换为雄辩的查询

时间:2018-11-19 07:08:20

标签: laravel eloquent

enrich_chat_message中选择DISTINCT rich_customers.id     INNER JOIN rich_customers ON rich_chat_message.sent_by = enrich_customers.id     || rich_chat_message.received_by = enrich_customers.id     其中(enrich_chat_message.sent_by = 3     || rich_chat_message.received_by = 3)和rich_customers.id!= 3

将原始查询转换为雄辩的查询

enrich_chat_message内联接中选择DISTINCT rich_customers.id进入rich_chat_message.sent_by = enrich_customers.id || rich_chat_message.received_by = enrich_customers.id其中(enrich_chat_message.sent_by = 3 || rich_chat_message.received_by = 3)和rich_customers.id!= 3

收件人:

DB::table('chat_message')
    ->select('customers.id')
    ->JOIN('customers','chat_message.received_by','=','customers.id','||','customers','chat_message.sent_by','=','customers.id')
    ->where('chat_message.sent_by',3)                         
    ->orWhere(function($q) use ($authid){
        $q->where('chat_message.sent_by',3)
        ->orwhere('chat_message.received_by',3);
    })->where('chat_message.sent_by','!=', 3)                       
    ->distinct()->get();

1 个答案:

答案 0 :(得分:1)

DB::table('chat_message') ->JOIN('customers','chat_message.received_by','=','customers.id','||','customers','chat_message.sent_by','=','customers.id')
->select('customers.id')
->where('chat_message.sent_by',3)                         
                 ->orWhere(function($q) use ($authid){
                          $q->where('chat_message.sent_by',3)
                          ->orwhere('chat_message.received_by',3);
                      })->where('chat_message.sent_by','!=', 3)                       
                 ->groupBy(customers.id)->get();

尝试groupBy而不是与众不同