Laravel 更新方法通过模型名称而不是 ID

Laravel update method passing through model name instead of ID

我在调用更新方法时遇到资源路由问题。

我收到这个错误:

Creating default object from empty value

控制器:

public function update($id)
{
    $input = Input::all();
    $validation = Validator::make($input, Vehicle::$rules, Vehicle::$messages);

    if ($validation->passes())
    {
        $this->vehicle->update($id, $input);
        return Redirect::route('admin.vehicles.index')->with('success', 'Car Updated');
    }

    return Redirect::back()
        ->withInput()
        ->withErrors($validation);
}

存储库:

public function update($id, $input)
{
    $vehicle = Vehicle::find($id);
    $vehicle->VRM = $input['VRM'];
    $vehicle->make = $input['make'];
    $vehicle->model = $input['model'];
    $vehicle->description = $input['description'];
    $vehicle->save();
}

路线:

Route::resource('/admin/vehicles', 'VehiclesController');

如果我打印 ID,它会显示 {vehicle}。

我的表格是这样的:

{{ Form::open(['route' => 'admin.vehicles.update', 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}

    // input fields etc

{{ Form::close() }}

我觉得可能是表格有问题?因为当错误被抛出时 URL 是:

http://localhost/admin/vehicles/%7Bvehicles%7D

在 CRUD 应用程序中使用资源路由之前从未遇到过任何问题,看不出哪里出了问题?

您需要更新路线中的 id...

{{ Form::open(['route' => array('admin.vehicles.update', $vehicle->id), 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}