dskzap8756 2018-08-19 14:04
浏览 124

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

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!

  • 写回答

3条回答 默认 最新

  • dshgnt2008 2018-08-19 14:26

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?