Laravel 5.0 路由模型绑定在销毁操作中不起作用

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

我有一个带有一个提交按钮的表单,并在控制器中设置了销毁方法的操作。相同的代码适用于其他表单和控制器,但不适用于这个。当我在 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中的销毁方法:

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); 行甚至不起作用,因为表格没有 link 到正确的路线。

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

.

.

提交表格后:

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

我在routes.php中使用Route::resource

请帮忙,我是 laravel 的新手。

你是正确的,表单标签中的操作是错误的。

试试这个:

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

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

您没有为 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() !!}

嘿,这取决于你的路由 如果你使用 Route::resource('companies','CompaniesController'); 对你来说很容易,不需要麻烦你可以像这样使用你的按钮

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

如果你想使用你的方式,那么你必须像这样使用你的表单

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