user9717203
user9717203

Reputation:

Laravel - Query a model when calling all

Is it possible in laravel to call a collection using

$flights = App\Flight::all();

but getting all that match a query for example

$flights = App\Flight::all()->where('pilotID', flightCaptainId);

Upvotes: 0

Views: 146

Answers (2)

Trần Huế Minh
Trần Huế Minh

Reputation: 1

You can use the filter() function


    $flights = App\Flight::all();
    $flights = $flights->filter(function($flight) use ($flightCaptainId)
                   {
                       return $flight->pilotID == $flightCaptainId;
                   });

Upvotes: 0

Mahdi Younesi
Mahdi Younesi

Reputation: 7509

Use get()

$flights = App\Flight::where('pilotID', flightCaptainId)->get();

Upvotes: 2

Related Questions