【问题标题】:Laravel 5.2 soft delete does not workLaravel 5.2 软删除不起作用
【发布时间】:2016-07-22 04:27:52
【问题描述】:

我有一个带有表格和模型的简单应用程序:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use SoftDeletes;
class Post extends Model
{
   protected $table = 'post';

   protected $dates = ['deleted_at'];

   protected $softDelete = true;

}

我正在尝试制作软删除的示例,并且我正在使用路由,例如 route.php:

<?php
use App\Post;

use Illuminate\Database\Eloquent\SoftDeletes;
Route::get('/delete', function(){
    $post = new Post();
    Post::find(12)->delete();

});

我有一个使用迁移创建的列“created_at”:

    Schema::table('post', function (Blueprint $table) {
        $table->softDeletes();
    });

,但不是在此列中添加时间,而是当我运行该站点时,它会删除具有选定 ID 的行。我哪里错了?

【问题讨论】:

  • 尝试在班级内移动use SoftDeletes;。我认为文档表明

标签: laravel laravel-5 eloquent soft-delete


【解决方案1】:

您需要在模型中使用 SoftDeletes 特征,如下所示:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
   use SoftDeletes;

   protected $table = 'post';

   protected $dates = ['deleted_at'];
}

现在你没有应用特质,所以显然它不起作用。

此外,您的路线文件中还有一些不必要的代码。它应该是这样的:

<?php
use App\Post;

Route::get('/delete', function(){
    Post::find(12)->delete();
});

【讨论】:

  • 非常感谢 :) .
  • 啊,谢谢它在我尝试使用trashed() 方法时使用delete() 方法对我有用,该方法已在softDelete 的官方文档中显示,但这并没有做这项工作,总是返回false
  • 仍然可以用forceDelete方法永久删除;)
猜你喜欢
  • 2014-10-19
  • 1970-01-01
  • 2016-12-26
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 2017-05-22
相关资源
最近更新 更多