php Laravel 软删除帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22426165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Soft Delete posts
提问by DolDurma
in our project we must be use soft delete for each posts. in laravel document i think we can only use this feature for tables.
在我们的项目中,我们必须对每个帖子使用软删除。在 Laravel 文档中,我认为我们只能将此功能用于表格。
can we use that for posts on table such as
我们可以将它用于桌子上的帖子,例如
$id = Contents::find($id);
$id->softDeletes();
回答by The Alpha
When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, specify the softDelete property on the model (Documentation) :
软删除模型时,它实际上并未从数据库中删除。相反,在记录上设置了一个 Deleted_at 时间戳。要为模型启用软删除,请在模型上指定 softDelete 属性(文档):
Prior to Version 4.2 (But not 4.2 & Later)
4.2 版之前(但不是 4.2 及更高版本)
For example (Using a poststable and Postmodel):
例如(使用posts表和Post模型):
class Post extends Eloquent {
protected $table = 'posts';
protected $softDelete = true;
// ...
}
To add a deleted_atcolumn to your table, you may use the softDeletes method from a migration:
要向表中添加deleted_at列,您可以使用迁移中的 softDeletes 方法:
For example (Migration class' upmethod for poststable) :
例如(迁移类的表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();
});
}
Now, when you call the deletemethod on the model, the deleted_atcolumn will be set to the current timestamp. When querying a model that uses soft deletes, the "deleted" models will not be included in query results. To soft deletea model you may use:
现在,当您delete在模型上调用该方法时,该deleted_at列将设置为当前timestamp. 查询使用软删除的模型时,“已删除”的模型将不会包含在查询结果中。对于soft delete您可以使用的模型:
$model = Contents::find( $id );
$model->delete();
Deleted (soft) models are identified by the timestampand if deleted_atfield is NULLthen it's not deleted and using the restoremethod actually makes the deleted_atfield NULL. To permanently delete a model use forceDeletemethod.
删除的(软)模型由timestamp和 如果deleted_at字段标识,NULL则它不会被删除,并且使用该restore方法实际上使deleted_at字段NULL。永久删除模型使用forceDelete方法。
Updated Version (Version 4.2):
更新版本(版本 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';
// ...
}
Updated Version (Version 5.0 & Later):
更新版本(版本 5.0 及更高版本):
use Illuminate\Database\Eloquent\SoftDeletes; // <-- This is required
class Post extends Eloquent {
use SoftDeletes; // <-- Use This Instead Of SoftDeletingTrait
protected $table = 'posts';
// ...
}
回答by majidarif
You actually do the normal delete. But on the model you specify that its a softdelete model.
你实际上做了正常的删除。但是在模型上,您指定它是一个软删除模型。
So on your model add the code:
所以在你的模型上添加代码:
class Contents extends Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
Then on your code do the normal delete like:
然后在您的代码上执行正常的删除操作,例如:
$id = Contents::find( $id );
$id ->delete();
Also make sure you have the deleted_atcolumn on your table.
还要确保你deleted_at的桌子上有列。
Or just see the docs: http://laravel.com/docs/eloquent#soft-deleting
回答by ajacian81
Just an update for Laravel 5:
只是 Laravel 5 的更新:
In Laravel 4.2:
在 Laravel 4.2 中:
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Post extends Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
becomes in Laravel 5:
在 Laravel 5 中变成:
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
protected $dates = ['deleted_at'];
回答by Unai Susperregi
In Laravel 5.5 Soft Deleted works ( for me ).
在 Laravel 5.5 软删除作品中(对我来说)。
Data Base
数据库
deleted_atField, default NULLvalue
Deleted_at字段,默认NULL值
Model
模型
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
}
Controller
控制器
public function destroy($id)
{
User::find($id)->delete();
}
回答by Inamur Rahman
In the Latest version of Laravel i.e above Laravel 5.0. It is quite simple to perform this task. In Model, inside the class just write 'use SoftDeletes'. Example
在 Laravel 的最新版本中,即Laravel 5.0 以上。执行此任务非常简单。在模型中,在类中只写“use SoftDeletes”。例子
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
And In Controller, you can do deletion. Example
在控制器中,您可以进行删除。例子
User::where('email', '[email protected]')->delete();
or
或者
User::where('email', '[email protected]')->softDeletes();
Make sure that you must have 'deleted_at' column in the users Table.
确保用户表中必须有“deleted_at”列。
回答by firelyu
Here is the details from laravel.com
这是来自 laravel.com 的详细信息
http://laravel.com/docs/eloquent#soft-deleting
http://laravel.com/docs/eloquent#soft-deleting
When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, specify the softDeleteproperty on the model:
软删除模型时,它实际上并未从数据库中删除。相反,在记录上设置了一个 Deleted_at 时间戳。要为模型启用软删除,请在模型上指定softDelete属性:
class User extends Eloquent {
protected $softDelete = true;
}
To add a deleted_at column to your table, you may use the softDeletesmethod from a migration:
要将 deleted_at 列添加到您的表中,您可以使用softDeletes迁移中的方法:
$table->softDeletes();
Now, when you call the delete method on the model, the deleted_at column will be set to the current timestamp. When querying a model that uses soft deletes, the "deleted" models will not be included in query results.
现在,当您在模型上调用 delete 方法时,deleted_at 列将设置为当前时间戳。查询使用软删除的模型时,“已删除”的模型将不会包含在查询结果中。

