I need to combine two queries into one so i can call it one time on my foreach inside a table in my blade view. I tried using merge() but it displays two separate queries. CountofPatents are all the patents that were submitted by a user and CountofApprovedPatents are the patents which has the status of "2". Here's how it looks now:
array 0 [person_id : 1
CountofApprovedPatents: 4
status: 2]array 1 [person_id : 2
CountofApprovedPatents: 2
status: 2]array 2 [person_id : 1
CountofPatents: 5]array 3 [person_id : 2
CountofPatents: 4]
Here's how it's supposed to look like
array 0 [person_id : 1
CountofApprovedPatents: 4
CountofPatents: 5]array 1 [person_id : 2
CountofApprovedPatents: 2
CountofPatents: 4]
Here's my code so far
AdminController
$start = $request->input('start');
$end = $request->input('end');
$approvedPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofApprovedPatents'))
->where([
['status', '=', 2],
['date_approved', '>=', $start],
['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();
$totalPatents = DB::table('patents')
->select('person_id', DB::raw('count(*) as CountofPatents'))
->where([
['date_approved', '>=', $start],
['date_approved', '<=', $end],
])
->groupBy('person_id')
->orderBy('status', 'desc')
->get();
$patents = $approvedPatents->merge($totalPatents);
$results = $patents->all();
return view('admin.patents.showPatents', compact('results',
'start', 'end'));
Is it possible to combine these two queries?
$results = DB::table('patents')
->union($approvedPatents)
->union($totalPatents)
->get();
statusgo?status = 2apply to all rows?CountofApprovedPatents, because the other query disregards the status value.CountofPatentscounts all associated with theperson_idwhether thestatusis 1 or 2.status = 2doesn't apply to all?