how do I filter data from parameters, for example I need data to filter from branch from dropdown with its params : /dashboard?branch=1 example.
this the Services i use :
class DashboardService
{
public function dataIndex(string $branch = null): array
{
/**
* branch select filter
*/
$branches = Branch::all();
/**
* User role count
*/
$roles = Role::whereIn('name', ['Cashier', 'Operator'])->pluck('name');
$countUserByRole = [];
foreach ($roles as $role) {
$count = User::role($role)->count();
$usersCountByRole[$role] = $count;
}
$totalVehicle = Vehicle::count();
$totalUser = User::role('Customer')->count();
/**
* Membership
*/
$totalRemainingQuota = UserMembership::sum('remaining_quota');
$totalMembership = UserMembership::where('remaining_quota', '>', 0)
->distinct('user_id')
->count('user_id');
/**
* Branch
*/
$totalBranch = Branch::count();
/**
* Product
*/
$productTypes = Product::groupBy('product_type')->pluck('product_type');
$totalProductsByType = [];
foreach ($productTypes as $productType) {
$count = Product::where('product_type', $productType)->count();
$totalProductsByType[$productType] = $count;
}
$totalProduct = Product::count();
$totalProductDiscount = Discount::count();
return [
'usersCountByRole' => $usersCountByRole,
'totalUsers' => $totalUser,
'totalBranch' => $totalBranch,
'totalProduct' => $totalProduct,
'totalProductDiscount' => $totalProductDiscount,
'totalProductsByType' => $totalProductsByType,
'totalMembership' => $totalMembership,
'totalRemainingQuota' => $totalRemainingQuota,
'totalVehicle' => $totalVehicle,
'branchFilter' => $branches,
];
}
}