如何在路由中为 Laravel 中的 2 种不同控制器方法使用相同的 url?

How to use same url in routes for 2 different controller methods in a Laravel?

我有一个显示 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]);
    }
}

非常感谢任何帮助。

您在这里打破了 MVC 模式。这就是让你精神错乱的原因。

您的模型保存图表数据。控制器获取数据。您的视图显示了数据。

因此,如果您想在同一个 URL 上显示 X 个图表,请创建 X 个数据获取函数(最好通过模型 - MVC 中的 M)。然后,您在 return 视图的方法中获取该数据。然后你return所有图表的数据(可能是1,可能是6)到视图。和之前一样:

return view('dashboard', ['chart1_data' => $chart_data, 'chart2_data' => $chart_data]);

然后让您的 view 使用 @if 和诸如此类的东西决定要显示的图表。