【问题标题】:route not defined in laravel 8 resourcelaravel 8 资源中未定义路线
【发布时间】:2022-07-19 15:11:05
【问题描述】:

我想向我的资源控制器添加一个功能。我读过一些文章说我们必须将路线线放在资源线之前,这就是我所做的。但我仍然收到错误提示未定义路线。

    Route::name('panel.')->prefix('panel')->middleware('auth')->group(function () {

        Route::get('/dashboard', [DashboardController::class, 'index'])->name("dashboard");

        Route::resource('contact', ContactController::class)->only([
            'index', 'show', 'destroy'
        ]);
        Route::post('/portfolio' , [PortfolioController::class, 'visibility']);

        Route::resource('portfolio', PortfolioController::class)->except([
            'show'
        ]);

        Route::resource('customer', CustomerController::class)->except([
            'show'
        ]);

        Route::resource('advice', AdviceController::class)->only([
            'index', 'destroy'
        ]);

        Route::resource('invoice', InvoiceController::class)->only([
            'index', 'destroy', 'create', 'store',
        ]);
        Route::resource('email', EmailTemplateController::class)->only([
            'index', 'destroy', 'create', 'store',
        ]);

        Route::resource('profile', ProfileController::class)->only([
            'update', 'index', 'destroy'
            /*
             * index
             * destroy
             */
        ]);

        Route::get('/me', [ProfileController::class, 'show'])->name("profile.show");


    });

这是我的 web.php 路线的名称应该是 panel.portfolio.visibility。 还有一件事我没有为这个项目编写代码,我只是向它添加了一些功能。所以新功能是我的,但不是资源控制器。

    public function visibility(Request $request,$portfolio_id)
        {
            $portfolio= Portfolio::find($portfolio_id);
            if($portfolio instanceof Portfolio){
                $this->validate($request,[],[]);
                $indicator = ($request->input('custom-switch-checkbox') == 'on') ? 1 : 0;
                $newData= ['portfolio_visibility' => $indicator];
                $portfolio->update($newData);
                return redirect()->back()->with('success', 'با موفقیت به روز رسانی گردید.');
            }
        }

这是我的可见性功能。 此页面显示投资组合列表,并且有一列有一个开关,指示是否应显示投资组合。 我可以很好地创建投资组合,但我无法进入列表页面,这里是索引页面。

<td>
                                                    <!--dokme baraye namayesh -->
                                                    <form action="{{ route('panel.portfolio.visibility' , $portfolio->portfolio_id ) }}" role="form" method="post">
                                                        <label class="custom-switch mt-2" >
                                                            <input type="checkbox" name="custom-switch-checkbox"
                                                                   class="custom-switch-input"
                                                                   id="personal-data-button" {{($portfolio->portfolio_visibility== 0)? '': 'checked'}} onclick="{{ route("panel.portfolio.visibility" , $portfolio->portfolio_id ) }}">
                                                            <span class="custom-switch-indicator"></span>
                                                        </label>
                                                    </form>
                                                </td>

这是在 index.blade.php 我不知道为什么有问题。 我对 laravel 也很陌生,所以如果我的问题令人困惑或听起来很愚蠢,请善待。

【问题讨论】:

  • 路线名称应该是panel.portfolio.visibility 我没有看到任何这样命名的路线。您可以运行php artisan route:list 来查看您的所有路线及其名称

标签: php laravel-8


【解决方案1】:

当您使用 Route::resource 时,它​​会为您使用默认命名约定。如果您使用 Route::post,则必须像在仪表板路由中那样指定名称。

因此,以这种方式更改路线应该可行:

Route::post('/portfolio' , [PortfolioController::class, 'visibility'])->name('portfolio.visibility');

文档:https://laravel.com/docs/8.x/routing#named-routes

【讨论】:

    【解决方案2】:

    至于我,我注释掉了发生错误的代码。我删除了这些,一切都很好。

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2018-10-15
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2016-01-24
      • 2020-05-03
      • 2016-04-02
      相关资源
      最近更新 更多