How to add a custom method to resource controller in Laravel 5.6

What is the correct way of adding a custom method to a resource controller in Laravel 5.6?

What I have so far is a new method in my ProfileController:

public function approve($id){
    $user = User::find($id);
    $user->state = '1';
    $user->save();
    return redirect('/dashboard')->with('success', 'User approved.');
}

As well as the following lines added to my web.php file:

Route::post('/profile/{$id}/approve', 'ProfileController@approve');
Route::resource('profile', 'ProfileController');

The form in my view is (afaik) correctly rendered to:

<form method="POST" action="http://myurl.com/profile/10/approve" accept-charset="UTF-8">
    <input name="_token" type="hidden" value="v3F1RRhi7iJL2o4egOhcRiuahaGQBwkGkfMal1lh">
    <input name="_method" type="hidden" value="PATCH">
    <input class="btn btn-success" type="submit" value="Approve User">
</form>

Unfortunately nothing happens, except the "Sorry, the page you are looking for could not be found." page to be shown.

What am I missing? And to expand a bit on this question also, is this even a valid way to implement "single field updates" on a db entry?

Thank you for your help!

i see you have two problems: firstly correct the route like that

Route::post('/profile/{id}/approve', 'ProfileController@approve');

secondly you have to delete

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

or replace your route like that:

Route::patch('/profile/{id}/approve', 'ProfileController@approve');

Controllers - Laravel, Using the make:controller Artisan command, we can quickly create such a controller: The controller will contain a method for each of the available resource  A Laravel resource controller makes it easier to build RESTful controllers around resources. For instance, you may wish to create a controller that manages “blog posts” stored by your application. Normally, you use a laravel resource controller to group common routes in one class.

You would want to remove the $ sign from your route:

Route::post('/profile/{id}/approve', 'ProfileController@approve');

The rest of it is correct.

Laravel - Controllers, names and path defined by default with 7 different actions which are required for any application. You may register many resource controllers at once by passing an array to the resources method: Route::resources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]); Actions Handled By Resource Controller

You have written the parameter like var: $id, and you may write it without '$'. But really you can use the Laravel implicit model binding function to do this:

Route::post('/profile/{user}/approve', 'ProfileController@approve');

And then in your controller:

public function approve(User $user){
    // Delete this line--> $user = User::find($id);
    $user->state = '1';
    $user->save();
    return redirect('/dashboard')->with('success', 'User approved.');
}

How can i rename laravel controller with command line interface(CLI , Next, just add methods to your controller, prefixed with the HTTP verb they respond to: class UserController Customizing Resource Routes. Additionally, you  Adding Meta Data When Constructing Resources. You may also add top-level data when constructing resource instances in your route or controller. The additional method, which is available on all resources, accepts an array of data that should be added to the resource response:

Laravel Resource Controllers, Basic Controllers; Controller Filters; Implicit Controllers; RESTful Resource Next, just add methods to your controller, prefixed with the HTTP verb they respond  Creating custom class on Laravel is easy to apply. Often we need additional classes to summarize and facilitate us in building applications. Laravel is a PHP framework that implements a fairly robust MVC concept and a fairly viscous application of Object Oriented Programming.

Laravel, My doubt is how can I create a new function in this controller other then ://​laravel.com/docs/5.6/controllers#restful-supplementing-resource-  'guards' => [ 'api' => [ 'driver' => 'custom-token', ], ], Adding Custom User Providers. If you are not using a traditional relational database to store your users, you will need to extend Laravel with your own authentication user provider. We will use the provider method on the Auth facade to define a custom user provider:

HTTP Controllers - Laravel, How to create Laravel Resource Controller with resource route,route resource in laravel 5.4,simple laravel crud function with resource controller,php artisan  The static macro method allows you to add methods to the Collection class at run time. Refer to the documentation on extending collections for more information. make() The static make method creates a new collection instance. See the Creating Collections section. map()

Comments
  • Can you please do php artisan route:list and paste the output here?
  • stackoverflow.com/questions/16661292/…
  • Changing the route call to Route::Patch did the trick for me. However, is this a valid way to go? I could not find to many information about what the hidden input field _method is actually about and in particular, what the PATCH value means...?
  • PATCH is just rest method like POST, GET, PUT, DELETE, ETC... the input <input name="_method" help precise the method you want to use for your rest call because by default is just GET and POST method which are accepted
  • Once the $ has been removed, I now receive an "Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message". Is this related to the method spoofing input field I added?
  • @Quhalix89 because you are passing patch method in form but expecting post in routes.
  • I'd also want to thank you for your comment. Can you briefly describe what the benefit of using the implicit model binding function is? And regarding the form in the view, does it matter what data I provide in the form (currently $user->id)?
  • Model binding brings you multiple benefits. The first and most important is the clarification of concepts and the code readability. In your case, the most correct approach would be naming the route: ` Route::post('/profile/{user}/approve', 'ProfileController@approve')->name('user-approve'); ` Then, you can use it in your view in the way: ` action="{{route('user-approve', ['user' => $user])}}" ` Later, in the controller you don't need to do the search, Laraver do for you, and if the user does not exist, it automatically throws a ModelNotFoundException. Enjoy. Its amazing!