Laravel 5 路由模型绑定不工作

Laravel 5 Route Model Binding is not working

我正在尝试使用路由模型绑定但遇到了问题。

RouteServiceProvider.php

public function boot(Router $router)
{
    parent::boot($router);

    $router->model('categories', 'App\Category');
}

Route.php

Route::get('categories/trash', ['as' => 'admin.categories.trash', 'uses' => 'CategoriesController@trash']);
Route::get('categories/{categories}/restore', ['as' => 'admin.categories.restore', 'uses' => 'CategoriesController@restore']);
Route::get('categories/{categories}/delete', ['as' => 'admin.categories.delete', 'uses' => 'CategoriesController@delete']);
Route::resource('categories', 'CategoriesController');

CategoriesController.php

public function restore(Category $category)
{
    $category->restore();
    return redirect()->back();
}

public function delete(Category $category)
{
    $category->forceDelete();
    return redirect()->back();
}

查看

 <a href="{!! URL::route('admin.categories.restore', $category->id) !!}">Restore</a>
 <a href="{!! URL::route('admin.categories.delete', $category->id) !!}">Delete Permanently</a>

但是当我尝试 restoredelete 时,我遇到了 NotFoundHttpException

的问题

您评论中的屏幕截图表明您正在使用 SoftDeletes

以下模型绑定代码不考虑删除的行。

$router->model('categories', 'App\Category');

为了完成这个你需要使用 bind 而不是 model

$router->bind('categories', function($value)
{
    return App\Category::withTrashed()->where('id', $value)->first();
}

这将包括已删除的行。例如,您需要将其用于恢复路径。