one solution could be to write a helper function, eg like so:
public function filterMyDailyTracker($filtername)
{
$filter = \App\DailyTracker::query()->where('filtername', $filtername)->get();
return $filter;
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I wanted to create two separate data sets from same table with two different condition.
For e.g see below, this is what all I need.
$filter = "select * from filters"; $filter_one = $filter . " where filtername = 'test'; $filter_two = $filter . " where filtername = 'test1';
I did it in the following way using laravel.
$filter = \App\DailyTracker::query(); $filter_one = $filter; $filter_two = $filter;
$res1 = $filter_one->where('filtername', 'test')->get(); $res2 = $filter_two->where('filtername', 'test1')->get();
$res1 gets the record as I expected, however $res2 is null because the second condition appended to the first one.
I guess it is object reference and that may be the reason. Anyhow, is there any way to achieve my requirement so that I can avoid writing same line of code again and again.
@EliasSoares That won't work since newQuery creates an empty query.
The best way to solve it is as @frezno suggested, or clone the query manually:
$filterOne = clone $filter;
$filterTwo = clone $filter;
Please or to participate in this conversation.