Skip to content
Advertisement

How to make this type of query in Laravel?

This is the sql query but how can I do it in laravel?

SELECT id FROM clientes where email is null and phone is null and id in(SELECT DISTINCT(cliente_id) FROM reservas WHERE dia < now())

This is the code that I tried to make:

$usersIdDel = DB::table("clientes")->select('id')
            ->whereNotNull('email')->whereNotNull('phone')
            ->whereIn('id',function($query){            $query->select('cliente_id')->from('reservas')->distinct()->where('dia','<',date('Y-m-d'));
            })
            ->get();
      $clientes = Cliente::select()->orderBy('nombre', 'desc')->get();
        return view('admin.clientes.index')->with(compact('clientes'));

Advertisement

Answer

There are two queries anyway so..

 $clientIds =  DB::table('reservas')->select('cliente_id')->distinct()->where('dia','<',date('Y-m-d'))->get();

then,

$usersIdDel = DB::table("clientes")->select('id')
        ->whereNotNull('email')->whereNotNull('phone')
        ->whereIn('id', $clientIds->pluck('cliente_id')->all())
        ->get();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement