【发布时间】:2021-05-16 09:26:13
【问题描述】:
我对 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
web.php
Route::get('application/approve/{id}', 'CandidateController@postApprove')->name('application');
Route::get('candidate', [CandidateController::class, 'approve'])->name('candidate.approve');
Route::resource('candidates', CandidateController::class);
【问题讨论】:
-
这很可能是因为您的 web.php 文件中没有定义
candidate.approve的实际路由。当您指定路由名称时。用php artisan route:list检查你的路线,你会得到所有的路线 -
你的销毁路线是
candidates.destroyDocumentation -
@Tithira 我按照文档进行操作,但它说 Missing required parameters for [Route: Candidates.destroy] [URI: Candidates/{candidate}]
-
是的,您需要传递路由的参数,即
primary key或您的模型。默认情况下,id是主键。 Destroy 方法接受控制器函数中的主键并返回一个实例,因此在您的操作触发器Passing parameters for routes 中传递路由参数{{ route('candidates.destroy', ['candidate' => $candidate->id]) }} -
实际上我已将 id 放入我的操作触发器中但仍然无法正常工作