【问题标题】:How to use same url in routes for 2 different controller methods in a Laravel?如何在 Laravel 中的 2 个不同控制器方法的路由中使用相同的 url?
【发布时间】: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


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 哦,对了,对不起。感谢@Stan Smulders 的帮助
猜你喜欢
  • 2017-04-05
  • 2018-09-10
  • 2017-10-12
  • 1970-01-01
  • 2013-09-24
  • 2016-03-29
  • 2018-04-25
  • 2016-03-03
  • 2015-02-10
相关资源
最近更新 更多