Search code examples
phpformslaravelpatchput

Laravel's put/patch is not entering into my update method, appears error 404 page not found


I'm really desperate on trying to get the update method working, but it has been impossible, laravel doesn't enter the method I have tried everything

This is my route list: enter image description here

For my home controller I just generated the resource with

php artisan make:controller HomeController --resource

class HomeController extends Controller
{


public function index()
{

}


public function create()
{
    //
}

public function store(Request $request)
{

}


public function show($id)
{
    //
}

public function edit($id)
{
    //
}


public function update(Request $request, $id)
{
    return "Hello World";
}


public function destroy($id)
{
    //
}

}

THis is my web.php file: Route::resource(' ', 'HomeController');

This is my form

<form action="{{route('update', $user->id)}}" method="POST">
                      {{ method_field('PUT') }}    
                      {{ csrf_field() }}

            <tbody>
                <tr>
                    <td class="col-md-1" scope="row">{{ $user->id }}</td></th>                                
                    <td class="col-md-1"><input class="form-control" type="text" name="name" value="{{ $user->name }}" ></td>
                    <td class="col-md-1"><input class="form-control" type="text" name="phone" value="{{ $user->phone }}" ></td>
                    <td class="col-md-2"> <input class="form-control" type="text" name="email" value="{{ $user->email }}" ></td>
                    <td class="col-md-1"><input type="checkbox" {{ $user->hasRole('User') ? 'checked' : '' }} name="role_user"></td>
                    <td class="col-md-1"><input type="checkbox" {{ $user->hasRole('Agent') ? 'checked' : '' }} name="role_agent"></td>
                    <td class="col-md-1"><input type="checkbox" {{ $user->hasRole('Admin') ? 'checked' : '' }} name="role_admin"></td>
                    <td class="col-md-1"><input type="checkbox" {{ $user->user_active ? 'checked' : '' }} name="user_active"></td>

                    <td class="col-md-1"><button type="submit" value="{{ $user->id }}">Del</button></td>
                    <td class="col-md-1"><button type="submit" value="{{ $user->id }}">Edit</button></td>
                    <td class="col-md-1"><button type="submit" >Save</button></td>
                </tr>
            </tbody>
        </table>
 </form>

And when I click on submit (SAVE BUTTON) it redirects me to the local host address and the id of my user, but it never gets into update method!!! Just for testing purposes my update method should return a "Hello World" message

All I got is Laravel redirecting me to this address

http://localhost:8000/1

depending on my user ID in this case was 1

Any help would be appreciated.

Thanks a lot


Solution

  • I don't think your route definition will work with resource and empty string.

    Rather than empty string, use some string to represent given resource i.e. home and update your route definitions:

    https://laravel.com/docs/5.6/controllers#restful-naming-resource-routes

    Route::resource('home', 'HomeController')->names([
        'update' => 'home.update',
        // ...
    ]);
    

    Then you can use it with your form:

    <form action="{{route('home.update', $user->id)}}" method="POST">