X33
X33

Reputation: 1410

Laravel Resource Controller update method not firing

I have a resource controller TruckController generated via php artisan make:controller TruckController --resource.

When I go go to localhost/trucks/1/edit, the edit form shows up successfully.

When I click submit, I am redirected to the resource show page (localhost/trucks/1); however, the truck information is not updated and no errors were thrown.

It seems the update function is not firing at all, which I think means there is something wrong with the action attribute in my form, but I am still unsure. There shouldn't be an issue with the routes because the routes should already be defined because it's a resource controller.

edit.blade.php

                <form method="PUT" action="{{ action('TruckController@update', ['id' => $truck->id])  }}" class="validate form-horizontal form-groups-bordered" novalidate="novalidate">
                    @csrf
                    <input type="hidden" name="state" data-geo="administrative_area_level_1" value="{{ $truck->state }}">
                    <input type="hidden" name="city" data-geo="locality" value="{{ $truck->city }}">
                    <input type="hidden" name="zipcode" data-geo="postal_code" value="{{ $truck->zipcode }}">



                    [ ... ]
                </form>

TruckController@update

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(TruckCreateRequest $request, Truck $truck)
    {
        $validated = $request->validated();

        $truck->name = $request->name;
        $truck->description = $request->description;
        $truck->save();
    }

TruckCreateRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TruckCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name'              => 'required',
            'description'       => 'required',
            'city'              => 'required',
            'state'             => 'required',
            'zipcode'           => 'required',
            'address'           => 'required'
        ];
    }
}

And just in case this may be of more help ...

Web.php

<?php

Auth::routes();

// ajax
Route::post('ajax/notifications/clear', 'DashboardController@mark_notifications_read');

Route::get('/', 'DashboardController@index')->name('dash');

Route::resource('events', 'EventController');
Route::resource('venues', 'VenueController');
Route::resource('trucks', 'TruckController');
Route::resource('admin', 'AdminController');

App/Models/Truck.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Truck extends Model
{
    //
}

Upvotes: 0

Views: 771

Answers (2)

user10186369
user10186369

Reputation:

You should try this:

<form method="POST" action="{{ action('TruckController@update', [$truck->id])  }}" class="validate form-horizontal form-groups-bordered" novalidate="novalidate">
                    @csrf
                    <input type="hidden" name="state" data-geo="administrative_area_level_1" value="{{ $truck->state }}">
                    <input type="hidden" name="city" data-geo="locality" value="{{ $truck->city }}">
                    <input type="hidden" name="zipcode" data-geo="postal_code" value="{{ $truck->zipcode }}">



                    [ ... ]
                </form>

Upvotes: 1

piscator
piscator

Reputation: 8709

From the Laravel Documentation:

Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs.

Try to add a hidden method field to the form:

 @method('PUT')

Read more here: https://laravel.com/docs/5.7/blade#method-field

Upvotes: 2

Related Questions