Laravel 5 使用销毁方法删除现有文章

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39790082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:32:32  来源:igfitidea点击:

Laravel 5 Delete existing Article with destroy method

phplaravel

提问by Mamulasa

I am trying to delete an article with the following code:

我正在尝试使用以下代码删除一篇文章:

ArticlesController:

文章控制器:

public function destroy($id) {
  $article = Article::findOrFail($id);
  $article->delete();

  return redirect('backend/dashboard')->with([
    'flash_message' => 'Deleted',
    'flash_message_important' => false
  ]);
}

View:

看法:

@foreach($articles as $key => $article)
  <tr>
    <td class="td-actions text-right">
      <a  href="{{action('ArticlesController@edit',$article->id)}}"type="button" rel="tooltip" title="" class="btn btn-info btn-simple btn-xs" data-original-title="Edit Article">
        <i class="fa fa-edit"></i>
      </a>
      <a href="{{action('ArticlesController@destroy',$article->id)}}" type="button" rel="tooltip" title="" class="btn btn-danger btn-simple btn-xs" data-original-title="Delete Article">
        <i class="fa fa-times"></i>
      </a>
    </td>
  </tr>
@endforech

By clicking the "Delete Article" Button I am redirected to a total different view. It seems like the @show method is executed.

通过单击“删除文章”按钮,我被重定向到一个完全不同的视图。似乎执行了@show 方法。

My Routes:

我的路线:

Route::get('backend/articles/archive', 'ArticlesController@archive');
Route::resource('backend/articles', 'ArticlesController');
Route::get('backend/dashboard', [
  'middleware' => 'auth',
  'uses' => 'PagesController@dashboard'
]);

How can I fix this?

我怎样才能解决这个问题?

采纳答案by Sameer Shaikh

The reason is because you are using atag. Use the formtag with method equals to delete which will solve your problem.

究其原因是因为你使用一个标签。使用带有方法等于删除的表单标签,这将解决您的问题。

          @foreach($articles as $key => $article)
            <tr>
             <td class="td-actions text-right">
                <a  href="{{action('ArticlesController@edit',$article->id)}}"type="button" rel="tooltip" title="" class="btn btn-info btn-simple btn-xs" data-original-title="Edit Article">
                  <i class="fa fa-edit"></i>
                </a>

                {{ Form::open([ 'method'  => 'delete', 'route' => [ 'items.destroy', $item->id ] ]) }}
                    {{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
                {{ Form::close() }}

             </td>
            </tr>
            @endforech

回答by Komal

Try to do like this

尝试这样做

public function destroy($id) {
  $article = Article::findOrFail($id);
  $article->delete();

  return view('dashboard')->with([
    'flash_message' => 'Deleted',
    'flash_message_important' => false
  ]);
}

回答by Alexey Mezenin

You need to use deletemethod to call destroy()action, for example:

您需要使用delete方法来调用destroy()操作,例如:

{!! Form::open(['method' => 'Delete', 'route' => ['article.destroy', $id]]) !!}
<button type="submit" class="btn">Delete article</button>
{!! Form::close() !!}

You can't use a hreflink here.

你不能a href在这里使用链接。

回答by renov8

You are not passing the variables correctly.

您没有正确传递变量。

$url = action('UserController@profile', ['id' => 1]);

Source: https://laravel.com/docs/5.3/helpers#method-action

来源:https: //laravel.com/docs/5.3/helpers#method-action