我創建了一個資源控制器。問題是當我在視圖中撰寫路線時,瀏覽器顯示未定義路線 [crops.restore]。我運行 php artisan route:list 命令,我意識到路由串列中不存在該路由。
我的控制器是這個
// Display all crops
public function index(Request $request)
{
if ($request->has('trashed')) {
$records = Crop::onlyTrashed()
->get();
} else {
$records = Crop::get();
}
return view('crops.index', compact('records'));
}
//restore a file
public function restore(Request $request, $id)
{
Crop::withTrashed()->find($id)->restore();
return redirect()->back();
}
我的路線
// Crop controllers
Route::resource('crops', 'CropsController');
我的觀點
@foreach ($records as $crops)
//code ...
@if(request()->has('trashed'))
<a class="btn btn-success btn-sm float-left mr-1" href="/crops/{{ $crops['id'] }}/edit">Edit</a>
<a href="{{ route('crops.restore', $crops->id) }}" hljs-string">">Restore</a>
@else
<form method="POST" hljs-keyword">float-left" action="{{ route('crops.destroy', $crops->id) }}">
@csrf
<input name="_method" type="hidden" value="DELETE">
<button type="submit" hljs-string">" title='Delete'>Delete</button>
</form>
@endif
@endforeach
我的代碼有什么問題?
uj5u.com熱心網友回復:
web.php只需在檔案中添加路線。例如:
Route::get('/restore', '\App\Http\Controllers\HomeController@restore');
uj5u.com熱心網友回復:
恢復在您的控制器中提供此方法:索引、存盤、編輯、更新、顯示、銷毀。如果您想重定向到您的自定義方法,例如恢復,您必須在此問題上重寫您的路線:
Route::get('/restore', [App\Http\Controllers\CropsController::class, 'restore']);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417677.html
標籤:
下一篇:使用ajax發布id的問題
