路线 [crops.restore] 未定义

Route [crops.restore] not defined

我创建了一个资源控制器。问题是当我在视图中编写路由时,浏览器显示 Route [crops.restore] not defined. I 运行 the 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) }}" class="btn btn-success">Restore</a>
@else
 <form method="POST" class="float-left" action="{{ route('crops.destroy', $crops->id) }}">
     @csrf
     <input name="_method" type="hidden" value="DELETE">
     <button type="submit" class="btn btn-danger delete btn-sm " title='Delete'>Delete</button>
 </form>
@endif

@endforeach

我的代码有什么问题?

只需在web.php文件中添加路由即可。例如:

Route::get('/restore', '\App\Http\Controllers\HomeController@restore');

Restore 在您的控制器中提供以下方法:索引、存储、编辑、更新、显示、销毁。如果你想重定向到你的自定义方法,比如 restore 你必须在这件事上重写你的路线:

Route::get('/restore', [App\Http\Controllers\CropsController::class, 'restore']);