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

Haider's avatar

db::select returning null at joins query

I am trying to fetch results from a joins query using DB::select( ). The query shows output in mysql phpmyadmin but not in laravel.

$q1=DB::select("Select id from vehicle"); // returns a single column of multiple ids
for($i=0; $i<=count($q1); $i++)
{
    $q2=DB::select("Select * from reservation inner join vehicle on (reservation.v_id=vehicle.id) where vehicle.id=?",[$q1[$i]->id]); 
    // returns null when $q[$i]->id is written and returns some data when an integer is given instead of $q1 object

}

Please help if you understood my problem.

0 likes
2 replies
Tray2's avatar

Like @Cronix said or if you are set on using SQL make it one query.

SELECT v.id v_id, r.*
FROM vehicle v, reservation r
WHERE  v.id = r.v_id;

A few tips always use plural for your tables like vehicles and reservations and don't abriviate foreign keys like v_id should be vehicle_id.

Please or to participate in this conversation.