首页 > 解决方案 > 未定义路线

问题描述

我对 laravel 很陌生,我对路由真的很糟糕。我想删除特定数据,但它说路线未定义

CandidateController.php 这是我删除的方法

public function destroy(Form $candidates)
{
    $candidates->delete();

    return redirect()->route('candidate.approve');
}

路线

Route::resource('candidates', CandidateController::class);

我正在使用资源,当我浏览本教程时,它会将我的代码缩短到上面。当我单击删除按钮时,它显示未定义路线 [candidate.approve]。有人可以帮我在哪里出错吗?

 @foreach ($candidates as $candidate)
                            <div class="modal__content">
                                <div class="p-5 text-center"> <i data-feather="x-circle" class="w-16 h-16 text-theme-6 mx-auto mt-3"></i>
                                    <form action="{{ route('candidates.destroy', $candidate->id) }}" method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <div class="text-3xl mt-5">Are you sure?</div>
                                        <div class="text-gray-600 mt-2">Do you really want to delete these records? This process cannot be undone.</div>

                                        <button type="button" data-dismiss="modal" class="button w-24 border text-gray-700 dark:border-dark-5 dark:text-gray-300 mr-1">Cancel</button> 
                                        <button type="submit" title="delete" class="button w-24 bg-theme-6 text-white" >Delete</button> 
                                </div>
                                <div class="px-5 pb-8 text-center"> 
                            </div>
                        </div>
                    </form>
                        </div>
                        @endforeach

网页.php

Route::get('application/approve/{id}', 'CandidateController@postApprove')->name('application');
Route::get('candidate', [CandidateController::class, 'approve'])->name('candidate.approve');
Route::resource('candidates', CandidateController::class);

标签: phplaravel

解决方案


只需在 Route::resource 之前添加带有Candidate.approve名称的新 Route。

你的 web.php 文件将是这样的

Route::get('your-url', [CandidateController::class, 'approve')->name('candidate.approve');
Route::resource('candidates', CandidateController::class);

但最好将 prural 用于命名路由,例如资源控制器:

  1. 候选人.create
  2. 候选人商店
  3. ...

更新

因为我知道应用程序的流程,你应该在控制器上使用它:

return back();

为什么?因为当管理员在模式上单击删除时,它将转到另一个 URL 以从数据库中删除数据。删除后,return back() 会将管理员重定向到以前的 URL


推荐阅读