Laravel 5.2软删除不起作用 [英] Laravel 5.2 soft delete does not work
问题描述
我有一个带有表帖子和模型的简单应用程序:
I am have simple app with a table post and model:
<?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为例使用路由:
I am trying to make example of soft delete and i am using route just for example route.php:
<?php
use App\Post;
use Illuminate\Database\Eloquent\SoftDeletes;
Route::get('/delete', function(){
$post = new Post();
Post::find(12)->delete();
});
我有一个通过迁移创建的"created_at"列:
I have a column "created_at" created with migration:
Schema::table('post', function (Blueprint $table) {
$table->softDeletes();
});
,但是当我运行该站点时,它没有删除时间到此列,而是删除了具有选定ID的行.我在哪里错了?
, but instead of adding time to this column, when I run the site it deletes the row with selected id. Where am I wrong ?
推荐答案
您需要在模型内部使用SoftDeletes
特征,如下所示:
You need to use SoftDeletes
trait inside your model like so:
<?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'];
}
现在,您没有应用特质,所以显然它不起作用.
Now you, are not applying trait, so obviously it doesn't work.
此外,您的路由文件中还包含不必要的代码段.它应该看起来像这样:
In addition you have unnecessary piece of code in your routes file. It should look like this:
<?php
use App\Post;
Route::get('/delete', function(){
Post::find(12)->delete();
});
这篇关于Laravel 5.2软删除不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!