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?
@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();
@tisuchi Thank you for your reply. But I am getting error with your code.
Table reserva not found.
@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();
Please or to participate in this conversation.