【问题标题】:Laravel delete button not workingLaravel删除按钮不起作用
【发布时间】:2017-11-27 05:48:29
【问题描述】:

我想知道是否有人可以查看此代码并告诉我为什么它不起作用。当我按下提交按钮时,它不会提交。

 {!! Form::open([
     'method' => 'DELETE',
     'route' => ['posts.destroy', $post->id],
     'style' => 'display: inline'
  ]) !!}

  {!! Form::submit('Delete this post?', ['class' => 'btn btn-danger']) !!}
 {!! Form::close() !!}  

我将它提交给 PostController 的 destory 方法,其中路由定义为“posts”。

路由文件

Route::group(['prefix' => 'admin'], function() {
    Route::resource('posts', 'PostController');

});

【问题讨论】:

  • 'method' => 'DELETE' 更改为'method' => 'POST'
  • @LarsMertens 为什么你认为这会有所帮助?
  • 我刚刚测试了你的代码,它很好,它在这里工作
  • @behz4d 正要回复同样的事情。我的评论只是一个猜测。 Kaley36 您的代码很好,但发生了上面示例中未定义的其他事情。我唯一能想到的是admin.posts.destroy,如果不是,我们需要更多信息。
  • 即使路由无效,它仍然应该发布

标签: laravel-5 laravel-form


【解决方案1】:

改变

{!! Form::open([
 'method' => 'DELETE',
 'route' => ['posts.destroy', $post->id],
 'style' => 'display: inline'
 ]) !!}

 {!! Form::submit('Delete this post?', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

{!! Form::open([
 'method' => 'DELETE',
 'route' => ['admin.posts.destroy', $post->id],
 'style' => 'display: inline'
  ]) !!}

 {!! Form::submit('Delete this post?', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}  

在路由中遵循您的前缀。希望对您有所帮助

【讨论】:

  • 这是错误的,他正在使用 Laravel HTML 集合,它会自动为 'method' => 'DELETE', 生成隐藏字段,所以他不需要再次使用 {{ Form::hidden('_method', 'DELETE') }} 进行操作,使用 laravel 时它会自动出现HTML 集体和类似DELETE 的方法
  • 不确定发生了什么,但我清除了缓存并重写了表单,现在它可以工作了,即使它的代码相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
相关资源
最近更新 更多