我有一个简单的应用程序,有一个表格和模型:
<?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的行。我哪里错了?
答案 0 :(得分:7)
您需要在模型中使用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();
});