Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kaizokupuffball's avatar

Query to eloquent converter

Hello

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'));

Any tips?

0 likes
4 replies
tisuchi's avatar

@kaizokupuffball

I can think of two queries here. I gave you a rough idea here-

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

$usersIdDel = DB::table("clientes")->select('id')
        ->whereNotNull('email')->whereNotNull('phone')
        ->whereIn('id', $clientIds->pluck('cliente_id')->all())
        ->get();
1 like
tisuchi's avatar
tisuchi
Best Answer
Level 70

@kaizokupuffball

Sorry. It has typo. I forgot to use s end of the table.

Try this-

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

$usersIdDel = DB::table("clientes")->select('id')
        ->whereNotNull('email')->whereNotNull('phone')
        ->whereIn('id', $clientIds->pluck('cliente_id')->all())
        ->get();
1 like

Please or to participate in this conversation.