【发布时间】:2021-11-05 04:56:48
【问题描述】:
我有一个显示 4 个图表统计数据和一个面积图的仪表板。我知道在这样的路由中使用相同的 URI 是不可能的:
Route::get('dashboard', [DashboardController::class, 'createChartStats']);
Route::get('dashboard', [DashboardController::class, 'createChartMonthlyInvoicesAndSales']);
我还没有创建createChartMonthlyInvoicesAndSales() 方法,但是有没有办法我仍然可以使用dashboard URI,这样我就可以在一页上显示这些图表?
这是我的控制器:
<?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]);
}
}
非常感谢任何帮助。
【问题讨论】:
-
如果你使用相同的 URL 会产生冲突
-
是的,我知道,还有其他方法可以让我的 URL 中仍然包含 myweb.app/dashboard 吗?我指的是
/dashboard -
这样做的目的是什么?如果您在浏览器中输入
yourdomain/dashboard,应该匹配哪个路由?为什么? -
使用指向 one 动作的 one 路线,并在该动作/方法中添加任意数量的图表
-
要显示 createChartStats() 图表,请参见屏幕截图:ibb.co/bQV34FF 然后下面的图表如下所示,请参见屏幕截图:ibb.co/qC0PvcN 在同一 /dashboard 页面上。
标签: laravel charts routes controller apexcharts