On my view i have 4 dropdown selects where the user can select any of them, a combination of any, or all of them, clicks on a button and it queries the db and returns the result. In trying to implement this, I thought of using different queries for different dropdowns, but I can't seem to get the query that should execute when all is selected.
$stat = request('status');
$network = request('network');
$search = request('search');
$dt = request('date');
$startDay = Carbon::now()->startOfDay();
$endDay = Carbon::now()->endOfDay();
$startWeek = Carbon::now()->startOfWeek();
$endWeek = Carbon::now()->endOfWeek();
$startMonth = Carbon::now()->startOfMonth();
$endMonth = Carbon::now()->endOfMonth();
if ($dt == 'today') {$date = [$startDay, $endDay];} elseif ($dt == 'week') {$date = [$startWeek, $endWeek];} elseif ($dt == 'month') {$date = [$startMonth, $endMonth];} else { $date = null;}
if (!$network && !$search && !$stat && !$date) {
$transactions = auth()->user()->orders()->latest()->paginate(10);
} elseif ($network) {
$transactions = auth()->user()->orders()
->whereHas('product.category', function ($query) use ($network) {
$query->where('name', $network);
})
->latest()->paginate(10);
} elseif ($stat) {
if ($stat == 'success') {$status = 1;} elseif ($stat == 'failed') {$status = 0;} else { $status = 1;}
$transactions = auth()->user()->orders()
->where('paid', $status)
->latest()->paginate(10);
} elseif ($date) {
if ($dt == 'today') {$date = [$startDay, $endDay];} elseif ($dt == 'week') {$date = [$startWeek, $endWeek];} elseif ($dt == 'month') {$date = [$startMonth, $endMonth];} else { $date = [$startMonth, $endMonth];}
$transactions = auth()->user()->orders()
->wherebetween('created_at', $date)
->latest()->paginate(10);
} elseif ($stat || $network || $date) {
$status = $stat == 'success' ? 1 : $stat == 'failed' ? 0 : 1;
if ($dt == 'today') {$date = [$startDay, $endDay];} elseif ($dt == 'week') {$date = [$startWeek, $endWeek];} elseif ($dt == 'month') {$date = [$startMonth, $endMonth];} else { $date = [$startMonth, $endMonth];}
$transactions = auth()->user()->orders()
->where('paid', $status)
->whereHas('product.category', function ($query) use ($network) {
$query->where('name', $network);
})->wherebetween('created_at', $date)
->latest()->paginate(10);
} elseif ($search) {
$transactions = auth()->user()->orders()
->where('phone', 'LIKE', "%$search%")
->latest()->paginate(10);
} else {
$transactions = auth()->user()->orders()->latest()->paginate(10);
}
return view('dashboard.customers.transactions', compact('transactions'));
This is my code right now, please help me with a much simpler way to achieve this, I'll be so grateful