1

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

Diego
  • 304
  • 4
  • 16

2 Answers2

2

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">
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
-1
    {{ Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>'col-md-12')) }}
.... wathever code here
{{ Form::close() }}

also change method action as PUT

OR

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="POST">

<!-- Rendered blade HTML form use this hidden. Dont forget to put the form method to POST -->

<input name="_method" type="hidden" value="PUT">

<div class="form-group">
    <textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
</div>
</form> 
Ravindra Bhanderi
  • 2,478
  • 4
  • 17
  • 29