Laravel Soft删除帖子

Dol*_*rma 19 php laravel laravel-4

在我们的项目中,我们必须对每个帖子使用软删除.在laravel文档中,我认为我们只能将此功能用于表格.

我们可以将它用于桌面上的帖子,例如

$id = Contents::find( $id );
$id->softDeletes();
Run Code Online (Sandbox Code Playgroud)

The*_*pha 64

软删除模型时,实际上并未从数据库中删除它.而是在记录上设置deleted_at时间戳.要为模型启用软删除,请在模型上指定softDelete属性(文档):

版本4.2之前(但不是4.2及更高版本)

例如(使用posts表和Post模型):

class Post extends Eloquent {

    protected $table = 'posts';
    protected $softDelete = true;

    // ...
}
Run Code Online (Sandbox Code Playgroud)

要向表中添加deleted_at列,可以使用迁移中的softDeletes方法:

例如(表的Migration class' up方法posts):

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function(Blueprint $table)
    {
        $table->increments('id');
        // more fields
        $table->softDeletes(); // <-- This will add a deleted_at field
        $table->timeStamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

现在,当您delete在模型上调用方法时,该deleted_at列将设置为当前timestamp.查询使用软删除的模型时,"已删除"模型将不会包含在查询结果中.对于soft delete您可能使用的模型:

$model = Contents::find( $id );
$model->delete();
Run Code Online (Sandbox Code Playgroud)

删除(软)模型由timestampif和if deleted_at字段标识,NULL然后它不被删除,并且使用该restore方法实际上是该deleted_at字段NULL.永久删除模型使用forceDelete方法.

更新版本(版本4.2):

use Illuminate\Database\Eloquent\SoftDeletingTrait; // <-- This is required

class Post extends Eloquent {

    use SoftDeletingTrait; // <-- Use This Insteaf Of protected $softDelete = true;

    protected $table = 'posts';

    // ...
}
Run Code Online (Sandbox Code Playgroud)

更新版本(5.0及更高版本):

use Illuminate\Database\Eloquent\SoftDeletes; // <-- This is required

class Post extends Eloquent {

    use SoftDeletes; // <-- Use This Instead Of SoftDeletingTrait

    protected $table = 'posts';

    // ...
}
Run Code Online (Sandbox Code Playgroud)


maj*_*rif 18

你实际上做了正常的删除.但是在模型上,您指定它是一个软删除模型.

所以在你的模型上添加代码:

class Contents extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

}
Run Code Online (Sandbox Code Playgroud)

然后在你的代码上执行正常删除,如:

$id = Contents::find( $id );
$id ->delete();
Run Code Online (Sandbox Code Playgroud)

还要确保你deleted_at的桌子上有专栏.

或者只看文档:http://laravel.com/docs/eloquent#soft-deleting


Ous*_*led 17

我刚刚用 Laravel 8 做了这个,它成功了。这基本上就是@The alpha 所说的,但试图更快地包装所有内容。请按照以下步骤操作。

迁移文件中添加:

$table->softDeletes();
Run Code Online (Sandbox Code Playgroud)

在模型中:

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
    ...
];
Run Code Online (Sandbox Code Playgroud)

}

在控制器中:

$user->delete();
Run Code Online (Sandbox Code Playgroud)

奖励:如果您需要恢复已删除的用户

User::withTrashed()->find($id);->restore();
Run Code Online (Sandbox Code Playgroud)


aja*_*n81 6

只是Laravel 5的更新:

在Laravel 4.2中:

use Illuminate\Database\Eloquent\SoftDeletingTrait;    
class Post extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

}
Run Code Online (Sandbox Code Playgroud)

在Laravel 5中成为:

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model {

    use SoftDeletes;
    protected $dates = ['deleted_at'];
Run Code Online (Sandbox Code Playgroud)