LearnLaravel
LearnLaravel

Reputation: 403

Laravel Resource default method - update

I have a controller called ItemController. With this controller, the method store and index are the only recognized methods. when i use the method update, it throws an error

Missing required parameters for [Route: items.update]

Route.php

            Route::resource('items', 'ItemsController');

ResourceRegistrar.php

protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];

html

 <form role="form" method="post"  action="{{ route('items.update') }}">
            {!! csrf_field() !!}

<div class="form-group" style="width: 400px;">
    <label for="exampleInputPassword1">Item Name</label>
     <input type="text" class="form-control" id="exampleInputPassword1" name="name" value="{!! $item->name !!}" placeholder="item name" required="">
</div>


<div class="form-group" style="width: 400px;">
       <label for="exampleInputPassword1">Retail Price</label>
                  <input type="number" step="any"  class="form-control" id="exampleInputPassword1"  value="{!! $item->retail_price !!}"  name="retail_price" placeholder="retail price" required="">
</div>


<div class="form-group" style="width: 400px;">
    <label for="exampleInputPassword1">Quantity Price</label>
                  <input type="number" step="any"  class="form-control" id="exampleInputPassword1"  value="{!! $item->quantity_price !!}" name="quantity_price"
                  placeholder="quantity price " required="">
</div>

     <button type="submit" class="btn btn-primary">Submit</button>


</form>

Controller

   public function update(Request $request, $id)
    {
        $item = Item::findorFail($id);

        $item->name = $request->get('name');
        $item->retail_price = $request->get('retail_price');
        $item->quantity_price = $request->get('quantity_price');

        $item->update($request, $id);

        Session()->flash('flash_message', 'Item successfully updated');
        return redirect()->route('items.index');
    }

How is this happening?

Upvotes: 0

Views: 642

Answers (2)

user320487
user320487

Reputation:

You need to be including a route parameter for the item you are updating. If you run

php artisan route:list

you should see something like: /items/{item} App\Http\Controllers\ItemController.

Your request URL should include the item id like:

`/items/1`

where 1 is the primary key for the item updating.

edit

You need to pass id to the route helper method in your HTML form:

action="{{ route('items.update', $item->id) }}"

Also, in your form, set the method as PUT or PATCH by:

<form>
    {!! method_field('put') !!}

    //or 

    {!! method_field('patch') !!}

    ...

</form>

Laravel uses a technique called Form Method Spoofing to fake PUT, PATCH, and DELETE HTTP verbs via POST.

edit 2

You're saving the record incorrectly, change

$item->update($request, $id);

to

$item->save();

Upvotes: 1

Nikola Gavric
Nikola Gavric

Reputation: 3543

When you call items.update route it expects the item you are updating, but you are not passing that item in the request, so the route throws an error because you are missing a required item to be able to know which one you are updating, does this make sense?

EDIT:

you need to include $item->id in your route('items.update', $item->id)

Upvotes: 1

Related Questions