In this example I will show you how to build an Eloquent query to filter results by certain field or fields of a table. In this example we will use simple Laravel Eloquent Model example to search a table and supply where condition on the fly, based on a given condition.
Example of Eloquent Model Query:
$query = ExampleModel::query(); if( $do_not_want_milestone_type ) { $query->where('type', '!=', ExampleModel::MILESTONE_TYPE); } $results = $query->get();
As shown in the example of Eloquent Model Query above if we do not want to include a certain ‘type’ in our results we can modify the query by injecting a where condition. This will exclude a particular record from the results.
The example above should apply to Laravel version 6, 7, 8, 9 and 10. You can easily implement conditional where in a Model query with help of this tutorial.
Read also: How to Export and Download CSV file without Plugin in Laravel 8/9/10