Laravel 5.2 软删除不起作用

Laravel 5.2 soft delete does not work

我有一个简单的应用程序 table post 和模型:

<?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 的行。我哪里错了?

您需要像这样在模型中使用 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();
});