【发布时间】:2014-04-20 23:50:49
【问题描述】:
在我们的项目中,我们必须对每个帖子使用软删除。在 laravel 文档中,我认为我们只能将此功能用于表格。
我们可以将它用于桌子上的帖子,例如
$id = Contents::find($id);
$id->softDeletes();
【问题讨论】:
在我们的项目中,我们必须对每个帖子使用软删除。在 laravel 文档中,我认为我们只能将此功能用于表格。
我们可以将它用于桌子上的帖子,例如
$id = Contents::find($id);
$id->softDeletes();
【问题讨论】:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model {
use SoftDeletes;
protected $table = 'posts';
// ...
}
软删除模型时,它实际上并没有从您的 数据库。相反,在记录上设置了
deleted_at时间戳。到 为模型启用软删除,指定softDelete属性 模型 (Documentation)。
use Illuminate\Database\Eloquent\SoftDeletingTrait; // <-- This is required
class Post extends Eloquent {
use SoftDeletingTrait;
protected $table = 'posts';
// ...
}
例如(使用posts 表和Post 模型):
class Post extends Eloquent {
protected $table = 'posts';
protected $softDelete = true;
// ...
}
要向表中添加 deleted_at 列,您可以使用
softDeletes迁移的方法:
例如(posts 表的迁移类'up 方法):
/**
* 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();
});
}
现在,当您在模型上调用 delete 方法时,deleted_at 列将设置为当前的 timestamp。查询使用软删除的模型时,“已删除”的模型不会包含在查询结果中。致soft delete您可能使用的模型:
$model = Contents::find( $id );
$model->delete();
删除的(软)模型由timestamp 标识,如果deleted_at 字段为NULL,则它不会被删除,使用restore 方法实际上会生成deleted_at 字段NULL。要永久删除模型,您可以使用forceDelete 方法。
【讨论】:
version 4.2 之前给出的,这是正确的,所以我认为我不应该对这个答案投反对票,但无论如何:-)
您实际上执行了正常的删除操作。但在您指定的模型上,它是一个软删除模型。
所以在你的模型上添加代码:
class Contents extends Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
然后在您的代码上执行正常删除,例如:
$id = Contents::find( $id );
$id ->delete();
还要确保您的桌子上有 deleted_at 列。
【讨论】:
$user->forceDelete();
delete 在正常模式下从数据库中删除。我认为forceDelete() 从Trash 中删除是吗?
use Illuminate\Database\Eloquent\SoftDeletingTrait;
只是 Laravel 5 的更新:
在 Laravel 4.2 中:
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Post extends Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
在 Laravel 5 中出现:
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
protected $dates = ['deleted_at'];
【讨论】:
在 Laravel 5.5 Soft Deleted 中有效(对我来说)。
数据库
deleted_at字段,默认NULL值
型号
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
}
控制器
public function destroy($id)
{
User::find($id)->delete();
}
【讨论】:
我刚刚用 Laravel 8 做到了这一点,并且成功了。这基本上是@The alpha 所说的,但试图更快地包装所有内容。请按照以下步骤操作。
在迁移文件中添加:
$table->softDeletes();
在模型中:
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
...
];
}
在控制器中:
$user->delete();
奖励:如果您需要恢复已删除的用户
User::withTrashed()->find($id);->restore();
【讨论】:
这是来自 laravel.com 的详细信息
http://laravel.com/docs/eloquent#soft-deleting
当软删除模型时,它实际上并没有从您的数据库中删除。相反,在记录上设置了 deleted_at 时间戳。要为模型启用软删除,请在模型上指定 softDelete 属性:
class User extends Eloquent {
protected $softDelete = true;
}
要向表中添加 deleted_at 列,您可以使用迁移中的 softDeletes 方法:
$table->softDeletes();
现在,当您在模型上调用 delete 方法时,deleted_at 列将被设置为当前时间戳。查询使用软删除的模型时,“已删除”的模型不会包含在查询结果中。
【讨论】:
在最新版本的 Laravel 中,即 Laravel 5.0 以上。执行此任务非常简单。 在模型中,在类中只写“使用 SoftDeletes”。示例
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
并且在Controller中,您可以进行删除。示例
User::where('email', 'youremail@example.com')->delete();
或
User::where('email', 'youremail@example.com')->softDeletes();
确保用户表中必须有“deleted_at”列。
【讨论】: