Laravel。通过删除请求传递参数

时间:2017-11-23 20:35:31

标签: php html laravel url

我正在使用LaravelCollective表单删除员工:

 {!! Form::open(['method' => 'DELETE', 'action' => ['EmployeesController@destroy', $employee->id, $company->id]]) !!}

                <div class="form-group">
                    {!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
                </div>

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

并希望传递2个参数:员工ID和公司ID。

我的路线:

Route::delete('/employees/{employee}/{company}', 'EmployeesController@destroy');

我的控制器功能:

public function destroy($id, $companyId)
{
    Employee::find($id)->delete();

    if($companyId == 0)
        return redirect('/employees');
    else
        return redirect('/companies/' . $companyId . "/edit");
}

我收到的错误是我只传递了1个参数。问题在哪里?

3 个答案:

答案 0 :(得分:0)

你必须在数组中发送params,尝试这样的事情。

{!! Form::open(['method' => 'DELETE', 'action' => ['EmployeesController@destroy', [$employee->id, $company->id] ]]) !!}

            <div class="form-group">
                {!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
            </div>

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

答案 1 :(得分:0)

变量名称应与route参数匹配。在控制器中尝试这样的事情:

public function destroy($employee, $company)
{
    Employee::find($employee)->delete();

    if($company == 0)
        return redirect('/employees');
    else
        return redirect('/companies/' . $company . "/edit");
    }
}

答案 2 :(得分:0)

请改为尝试:

// named route
Route::delete('employees/{employee_id}/{catetory_id}', 'EmployeesController@destroy')->name('employees.destroy');

// form using named route
{!! Form::open(['method' => 'DELETE', 'route' => ['employees.destroy', $employee->id, $company->id]]) !!}
    <div class="form-group">
        {!! Form::submit('Remove employee', ['class'=>'btn btn-danger']) !!}
    </div>
{!! Form::close() !!}