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';
    $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!

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘