Makwana
Makwana

Reputation: 70

Laravel eloquent Query Reuse extra condition

I have a query which gives me list of total counts of different items, as:

$data = DB::table($Table1)
           ->join($table2,$Table1.'.id','=',$Table2.'.device_key')
                ->where($Table1.'.created_at', '>', $value)
                ->select('item', DB::raw('count(*) as total'))
                ->groupBy('item')
                ->lists('total', 'item');

now i want to fetch same data with extra condition as >where($Table1.'.status', '=', 'SUCCESS') .

how do i do that ??

Upvotes: 0

Views: 1243

Answers (2)

DevK
DevK

Reputation: 9942

I don't think you'll get away with anything nice than this.

$query = DB::table($Table1)
           ->join($table2,$Table1.'.id','=',$Table2.'.device_key')
                ->where($Table1.'.created_at', '>', $value)
                ->select('item', DB::raw('count(*) as total'))
                ->groupBy('item');

$data1 = $query->lists('total', 'rem');
$data2 = $query->where($Table1 . '.status', '=', 'SUCCESS')->lists('total, 'rem');

Upvotes: 2

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17762

Use clone to copy the query object for modifications.

$query = DB::table()->where();

$clonedQuery = clone $query;
$clonedQuery->where(..);

Upvotes: 0

Related Questions