Search code examples
formslaravellaravel-5laravel-routinglaravel-form

Laravel 5.0 Route Model Binding didn't work in destroy action


I have a Form with one submit button and set the action to destroy method in a controller. The same code worked for other form and controller, but not working for this one. The action in form tag is wrong when I inspect my web page in Chrome.

This is my Form :

{!! Form::model($company, ['method' => 'PATCH', 'action' => ['Setting\Organization\CompaniesController@update', 'files'=>true, $company->CompanyCode]]) !!}
    <div class="form-group">
        {!! Form::label('CompanyCode', 'Company Code : ', ['class' => 'col-lg-3 col-md-3 col-sm-3 col-xs-3']) !!}
        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
            {!! Form::text('CompanyCode', null, ['class' => 'form-control', 'readonly' => true]) !!}
        </div>
    </div>

    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
        {!! Form::submit('Update Company', ['class' => 'btn btn-primary', 'id' => 'btnSubmit']) !!}
    </div>

{!! Form::close() !!}

{!! Form::model($company, ['method' => 'DELETE', 'action' => 'Setting\Organization\CompaniesController@destroy', $company->CompanyCode]) !!}

    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
        {!! Form::submit('Delete Company', ['class' => 'btn btn-danger']) !!}
    </div>

{!! Form::close() !!}

The Update button works fine.

I access this form via edit method in Setting\Organization\CompaniesController :

public function edit(Company $company){
    return view('setting.organization.company.edit', compact('company'));
}

Here's destroy method in Setting\Organization\CompaniesController :

public function destroy(Company $company){
    dd($company);
    //------------ delete company
    $company->IsActive = 0;
    $company->update();

    flash()->info('Company ' . $company->Name . ' has been deleted.');
    return redirect('company');
}

The dd($company); line doesn't even work because the form didn't link to the right route.

The RouteServiceProvider file :

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

    $router->bind('client', function($id){
        return \App\Models\Setting\ClientAccount::getClientFromAccountName($id);
    });

    $router->bind('company', function($id){
        return Company::getCompanyFromCode($id);
    });

}

.

.

This is the inspect element of the Form :

<form method="POST" action="http://localhost/hrmsystem/public/company/%7Bcompany%7D" accept-charset="UTF-8" com160202145801="COM160202145801">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="B2luMsN5Oy81GUFLoUCoHc2ERnqHe1AYir1DEY4N">

    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
        <input class="btn btn-danger" type="submit" value="Delete Company">
    </div>

</form>

.

.

After I submitted the form :

Error after submitting the form

I understand that the error is ModelNotFoundException because the passed CompanyCode is %7Bcompany%7D. But I don't know where is it come from.

I use Route::resource in the routes.php.

Please help, I'm new to laravel.


Solution

  • Hey its depend on your routing if you use Route::resource('companies','CompaniesController'); its easy for you no need to trouble just you can use your button like this

     {!! Form::open(array('class' => 'form-inline', 'method' => 'DELETE', 'route' => array('companies.destroy',  $company->CompanyCode))) !!}
                                    {!! Form::submit('DELETE', array('class' => 'btn btn-danger btn-xs')) !!}
                                    {!! Form::close() !!}
    

    if you want use your way then you have to use your form like this

    {!! Form::open('method' => 'DELETE', 'action' => ['Setting\Organization\CompaniesController@destroy', $company->CompanyCode]) !!}
        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
            {!! Form::submit('Delete Company', ['class' => 'btn btn-danger']) !!}
        </div>
    {!! Form::close() !!}