I have a dashboard that shows 4 chart stats and an area chart. I know it's impossible to use same URI in routes such as this:
Route::get('dashboard', [DashboardController::class, 'createChartStats']);
Route::get('dashboard', [DashboardController::class, 'createChartMonthlyInvoicesAndSales']);
I haven't created the createChartMonthlyInvoicesAndSales()
method yet, but is there a way I can still use dashboard
URI so I just can display these charts on one page?
Here's my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\{
Role,
Product,
Sales,
};
use Carbon\Carbon;
class DashboardController extends Controller
{
public function index()
{
if(Auth::user()->hasRole('cashier')) {
return view('cashier.dashboard');
} else {
return view('dashboard');
}
}
public function showCashierProfile()
{
return view('cashier.profile');
}
public function isAdministrator()
{
return Role::where('name', 'admin')->first();
}
public function getLowProducts(Request $request)
{
if($request->ajax()){
$products = Product::where('qty_on_hand', '<=', 10)->paginate(10);
return Response($products);
}
}
public function getTodaysInvoices()
{
$data = Sales::whereDate('created_at', Carbon::today()->toDateTimeString())
->where('payment_type', '=', 'credit')
->whereRaw('balance != FLOOR(0.00)');
$stats = [ 'total_balance' => $data->sum('balance'), 'total_count' => $data->count()];
return $stats;
}
public function getThisMonthsInvoices()
{
$data = Sales::whereMonth('created_at', Carbon::now()->month)
->where('payment_type', '=', 'credit')
->whereRaw('balance != FLOOR(0.00)');
$stats = ['total_balance' => $data->sum('balance'), 'total_count' => $data->count()];
return $stats;
}
public function getTodaysSales()
{
$data = Sales::whereDate('created_at', Carbon::today()->toDateTimeString())
->where('payment_type', '=', 'cash')
->whereRaw('balance = FLOOR(0.00)');
$stats = ['total_payment' => $data->sum('payment'), 'total_count' => $data->count()];
return $stats;
}
public function getThisMonthsSales()
{
$data = Sales::whereMonth('created_at', Carbon::now()->month)
->where('payment_type', '=', 'cash')
->whereRaw('balance = FLOOR(0.00)');
$stats = ['total_payment' => $data->sum('payment'), 'total_count' => $data->count()];
return $stats;
}
public function createChartStats() {
$chart_stats = [
'todays_invoices' => $this->getTodaysInvoices(),
'this_months_invoices' => $this->getThisMonthsInvoices(),
'todays_sales' => $this->getTodaysSales(),
'this_months_sales' => $this->getThisMonthsSales(),
];
return view('dashboard', ['chart_stats' => $chart_stats]);
}
}
Any help is much appreciated.
You're breaking the MVC pattern here. That's what's tripping you up mentally.
Your Model holds the chart data. The controller fetches the data. Your view shows the data.
So if you want to show X amount of charts on the same URL, create X data fetching functions (preferably through the Model(s) - the M in MVC). You then fetch that data in the method that returns the view. You then return the data of all the charts (could be 1, could be 6) to the view. As you did before:
return view('dashboard', ['chart1_data' => $chart_data, 'chart2_data' => $chart_data]);
And then have your view
decide what chart to show using @if
and whatnot.