Home > front end >  Custom model binding when using withTrashed method on Route declaration and resolveRouteBinding in M
Custom model binding when using withTrashed method on Route declaration and resolveRouteBinding in M

Time:12-04

I am having a problem with implicit model binding on routes for Soft Deleted Models. When using the withTrashed method, the resolveRouteBinding method in the Model is NOT called. When withTrashed method is removed from the route declaration, the method resolveRouteBinding is called as expected.

Steps To Reproduce:

routes/web.php

Route::get('user/{user}', function(SoftDeletedModel $user) {
    dd($user);
})->withTrashed(); //withTrashed is used

Models/SoftDeletedModel.php

use SoftDeletes;

public function resolveRouteBinding($value, $field = null) {
    dd("Successfully Substituted Bindings when using WithTrashed."); //This is not displayed.
    return parent::resolveRouteBinding($value, $field);
}

Is this a known bug or where am I going wrong? There also exists an issue on github

CodePudding user response:

You need to use this method resolveSoftDeletableRouteBinding instead of resolveRouteBinding.

    /**
     * Retrieve the model for a bound value.
     *
     * @param mixed $value
     * @param string|null $field
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function resolveSoftDeletableRouteBinding($value, $field = null)
    {
        
         return parent::resolveSoftDeletableRouteBinding($value, $field);
    }
  • Related