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

ncltours's avatar

filter data in Query 2 tables

Hi I am having trouble in filtering 2 tables. I have Two table called "Flights" and "MultiFlightSegments". "Flight" contains information about flights and "MultiFlightSegments" contains informations of "Flights" table. Basically "Flight" hasMany retations to "MultiFlightSegments" . When I select "Flight" I need to display "MultiFlightSegments" according to Flight.

How do you filter using query builder in laravel?

0 likes
1 reply
lostdreamer_nl's avatar

I dont think I understand your question exactly, but if so and you have your relations setup correctly you could do this:

// Flight.php 
public function multiflightsegments() 
{
    return $this->hasMany(MultiFlightSegment::class);
}


// some controller
$flight = Flight::with('multiflightsegments')->find(1);
dd( $flight->toArray() );

And it would give you the Flight object with id 1, including all its MultiFlightSegments.

Please or to participate in this conversation.