dst2007 2015-04-27 16:26
浏览 134
已采纳

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

I am having an issue with my resource route when calling the update method.

I get this error:

Creating default object from empty value

The controller:

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

repository:

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:

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

If I print the ID then it shows {vehicle}.

My form is this:

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

    // input fields etc

{{ Form::close() }}

I think there is something wrong with the form possibly? Since when the error is thrown the URL is:

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

Never had any issues before with using resource routes with CRUD applications and cant see where this is going wrong?

  • 写回答

1条回答 默认 最新

  • duanpao6163 2015-04-27 16:33
    关注

    You need the id in update route...

    {{ Form::open(['route' => array('admin.vehicles.update', $vehicle->id), 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路