Laravel 5 Route Model Binding不起作用

时间:2015-02-25 10:30:14

标签: php laravel-5

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

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

的问题

enter image description here

1 个答案:

答案 0 :(得分:2)

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

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

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

为了实现这一目标,您需要使用bind代替model

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

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