如何在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');

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

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

The rest of it is correct.

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.');
}