php - 未定义路线
问题描述
我对 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);
解决方案
只需在 Route::resource 之前添加带有Candidate.approve名称的新 Route。
你的 web.php 文件将是这样的
Route::get('your-url', [CandidateController::class, 'approve')->name('candidate.approve');
Route::resource('candidates', CandidateController::class);
但最好将 prural 用于命名路由,例如资源控制器:
- 候选人.create
- 候选人商店
- ...
更新
因为我知道应用程序的流程,你应该在控制器上使用它:
return back();
为什么?因为当管理员在模式上单击删除时,它将转到另一个 URL 以从数据库中删除数据。删除后,return back() 会将管理员重定向到以前的 URL
推荐阅读
- python - 您如何解析嵌套的 JSON 数据以获取特定信息?
- r - R:如何在函数中按 $ 而不是 [] 进行索引?
- java - 多个逗号分隔值在 Spark Java 中分隔行
- c# - 如何从服务层获取 HttpContext
- javascript - Javascript Uncaught TypeError: second.style 未定义
- python - 如何根据另一列的值拆分数据框特定列中的字符串?
- camera - gstreamer 如何选择相机的格式类型
- javascript - 防止在页面刷新站点后重置计时器
- html - 如何在 Outlook 电子邮件正文中的 converttohtml 之前隐藏动态子范围中的列?
- vue.js - 在Vue JS中刷新页面时找不到404