Juliano Rafael
Juliano Rafael

Reputation: 15

Eloquent Route implicit binding Laravel 5.6 isn't working

I have tried to develop an application and I'd like to use route binding, but something is wrong and I don't know what it is. Plz, look code below and help me what it is wrong.

Route

|        | PATCH    | api/v1/filial/{filial}                  |      | Genesis\Base\Filial\Controllers\FilialController@update                    | auth:api   |

Model

class Filial extends Model{

/**
 * @var string
 */
protected $table = "filiais"; ...

Controller

class FilialController extends BaseFormController{...
    public function update(FilialRequest $request, Filial $filial){
       dd($filial);
    }...

And then the output comes as a model empty. I don't know what it is wrong, parameter's, Model, Uri all these things matches. I'm using Laravel 5.6 since the beginning of this project.

Upvotes: 0

Views: 2622

Answers (1)

Alex Harris
Alex Harris

Reputation: 6392

Ensure you have everything setup properly, in your kernel.php you should have:

protected $routeMiddleware = [
    ...
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ...
];

You also need to ensure you have the bindings middleware in your routes:

Route::group(['middleware' => ['bindings'], function() {
    // routes
}

If that still isn't working I would opt to look into explicit route binding:

To register an explicit binding, use the router's model method to specify the class for a given parameter. You should define your explicit model bindings in the boot method of the RouteServiceProvider class:

public function boot()
{
    parent::boot();

    Route::model('filial', App\Filial::class);
}

Upvotes: 6

Related Questions