Laravel 5.0 Route Model Binding在destroy动作中不起作用

时间:2016-02-02 18:48:11

标签: forms laravel laravel-5 laravel-routing laravel-form

我有一个带有一个提交按钮的表单,并将操作设置为在控制器中销毁方法。相同的代码适用于其他表单和控制器,但不适用于此。当我在Chrome中检查我的网页时,表单标记中的操作是错误的。

这是我的表格:

{!! 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() !!}

“更新”按钮工作正常。

我通过Setting \ Organization \ CompaniesController:

中的编辑方法访问此表单
public function edit(Company $company){
    return view('setting.organization.company.edit', compact('company'));
}

这是Setting \ Organization \ CompaniesController中的destroy方法:

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');
}

dd($company);行甚至不起作用,因为表单没有链接到正确的路径。

RouteServiceProvider文件:

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);
    });

}

这是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>

我提交表格后:

Error after submitting the form

我理解错误是ModelNotFoundException,因为传递的CompanyCode是%7Bcompany%7D。但我不知道它来自哪里。

我在routes.php。

中使用Route::resource

请帮助,我是laravel的新手。

3 个答案:

答案 0 :(得分:1)

你说得对,表格标签中的动作是错误的。

请改为尝试:

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

错误是在动作数组中包含'files' => true,因为它不是动作的一部分。它是它自己的属性,因此它应该与动作数组/它之外分开。

答案 1 :(得分:1)

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() !!}

答案 2 :(得分:0)

您没有对DELETE

使用模型绑定
{!! 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() !!}