【问题标题】:Delete data from the database with Laravel 5.4使用 Laravel 5.4 从数据库中删除数据
【发布时间】:2017-11-29 03:50:28
【问题描述】:

我对 Laravel 框架有点陌生,我正在构建一个简单的博客。我可以创建博客、显示博客并显示所有博客的概览。现在我想删除一个博客。因此,我在视图中创建了一个删除按钮,其中包含一个路由链接,该链接也将传递文章的 ID。然后,在我的路由文件中,我指定了一个删除请求和一个控制器方法。在该方法中,我找到了 id 并尝试删除具有我在路由/视图中指定的 id 的行。

这不起作用。它不是激活destroy/delete 方法而是显示文章而不是删除它,并激活show 方法而不是delete 方法。谁能帮帮我,我做错了什么?

View.blade.php

<a href="{{route('nieuws.destroy', ['id' => $blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
  <i class="fa fa-trash"></i>
</a>

路线

Route::group(['middleware' => 'auth'], function () {

    Route::get('/aanvragen', 'aanvragenController@index')->name('aanvragen.index');

    Route::get('/logout' , 'Auth\LoginController@logout')->name('logout');

    Route::get('/nieuws/toevoegen', 'blogController@create')->name('blogs.add');

    Route::post('/nieuws/store', 'blogController@store')->name('nieuws.store');

    Route::delete('/nieuws/{id}', 'blogController@destroy')->name('nieuws.destroy');

});

Route::get('/nieuws', 'blogController@index')->name('blogs.index');

Route::get('/nieuws/{blog}', 'blogController@show')->name('blogs.show');

控制器方法

删除/销毁

public function destroy($id) {

    $blog = Blog::find($id);

    $blog->delete();

    return redirect('/nieuws');

}

显示

public function show(Blog $blog) {

    dd('show');


    return view('blogs.show', compact('blog'));

}

【问题讨论】:

  • @Gijsberts 您可能想重新检查 kunal 的答案。允许 GET 请求以任何方式更改数据是不好的做法和安全风险。

标签: php laravel laravel-5 laravel-5.4


【解决方案1】:

delete() 路由要求您发布数据。

HTML 表单仅支持 GET 和 POST,不支持 DELETE、PUT 等其他方法,这就是为什么 Laravel 使用 _method 来欺骗 HTML 表单不支持的方法。

希望在这些情况下使用 GET,因为有人可以在 IM 中或通过电子邮件向用户发送 URL (http://yoursite.com/blog/delete/1)。用户点击,博客消失。

按照使用资源控制器时的方式定义路由,因此:

Route::delete('/nieuws/{id}', 'blogController@destroy')->name('nieuws.destroy');

或者使用带有删除方法的表单:

// apply some inline form styles
<form method="POST" action="{{ route('nieuws.destroy', [$blog->id]) }}">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
    <button type="submit">Delete</button>
</form>

或者做一些 javascript 魔术,就像 SR_ 在他对您的 OP 的评论中发布的链接一样。


还有一件事,在您的销毁操作中添加某种验证。现在,当您提供不存在的 id 或其他内容时,您将收到 500 错误,而不是您想要的 404。

public function destroy($id)
{
    $blog = Blog::findOrFail($id);

    $blog->delete();

    return redirect('/nieuws');
}

【讨论】:

    【解决方案2】:

    我认为您需要更新您的销毁功能,例如:

    public function destroy($id) {
    
        $blog = DB::table('blog')->where('id',$id)->delete();
    
        return redirect('/nieuws');
    
    }
    

    并更新您的视图代码,例如:

    <a href="{{route('nieuws.destroy', [$blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
      <i class="fa fa-trash"></i>
    </a>
    

    希望这对你有用!

    【讨论】:

    • 很遗憾不是,因为我猜这不是问题。问题是我的控制器显示的是@show 方法而不是@destroy 方法
    • @Gijsberts blog in show 方法的结果是什么?
    • 只是博客的详细信息以及来自数据库的数据。出于测试目的,我现在使用 datadump 函数来查看它是否激活了 show 方法。
    【解决方案3】:

    我也是 Laravel 的新手,但我通过这种方式使它工作: (我使用'Article'作为模型的名称,路由中的resource“方法”代表一堆有用的路由,包括你写的路由)

    控制器:

    public function destroy($id){
            Article::find($id)->delete();
            //$article = Article::find($id);
            return redirect()->back()->withErrors('Successfully deleted!');
        }
    

    路线:

    Route::resource('article','ArticleController');
    

    但是,我认为问题在于模型的数据库名称的默认定义。 Laravel 会假设你有一个名为 blogs 的数据库,因为你有一个名为“blog”的模型。你的数据库名称对吗?

    【讨论】:

    • 是的。那不是问题,因为我还可以添加博客并展示它们。问题是销毁路由与显示路由冲突。它不会激活 destroy 方法,而是激活 show 方法。
    • 把视图改成这样怎么样:&lt;form action="{{ url('admin/blog/'.$blog-&gt;id) }}" method="POST" style="display: inline;"&gt; {{ method_field('DELETE') }} {{ csrf_field() }} &lt;button type="submit" class="btn btn-danger"&gt;delete&lt;/button&gt; &lt;/form&gt;
    【解决方案4】:

    要使用DELETE HTTP 动词,您的表单应包含POST 方法并设置method_field('DELETE')

    例子:

    <form method="POST" action="{{ route('xxx.destroy', $xxx->id) }}">
        {{ csrf_field }}
        {{ method_field('DELETE') }}
    </form>
    

    【讨论】:

      猜你喜欢
      • 2017-10-06
      • 2023-02-26
      • 2020-01-06
      • 2015-07-30
      • 2017-08-13
      • 2021-06-08
      • 2017-06-02
      • 2019-06-08
      • 1970-01-01
      相关资源
      最近更新 更多