The following is the structure of my database
id name country province status updated_at
1 A US TX 2 [timestamp]
2 B UK LON 1 [timestamp]
3 C US TX 2 [timestamp]
4 D US WA 2 [timestamp]
5 E US WA 3 [timestamp]
6 F US WA 2 [timestamp]
7 G US TX 1 [timestamp]
Information from the form (search criteria)
- country (US) in this example
- Start date, End date
I need to get the following count and information.
- Group by province {
- $ca = count(status '1')
- $cb = count(status '2')
- $cc = count(status '3') }
Expected result:: 'US' search criteria:-
Province TX
Status '1': 1
Status '2': 2
Status '3': 0
Province WA
Status '1': 0
Status '2': 2
Status '3': 1
The following is what i trying to do but failed
$users = DB::table('users')
->where('country', $b)
->select('province', DB::raw('count(*) as total'))
->groupBy('province')
->whereBetween('updated_at', [
\Carbon\Carbon::createFromDate($d, $e)->startOfMonth(),
\Carbon\Carbon::createFromDate($f, $g)->endOfMonth()
])->orderBy('created_at','desc')->get();