Lovelock
Lovelock

Reputation: 8085

Laravel update method passing through model name instead of 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?

Upvotes: 0

Views: 1205

Answers (1)

maztch
maztch

Reputation: 1697

You need the id in update route...

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

Upvotes: 2

Related Questions