在我们的项目中,我们必须对每个帖子使用软删除。在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
属性(文档)。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
。当查询使用软删除的模型时,“已删除”的模型将不会被包含在查询结果中。要进行软删除
,您可以使用:
$model = Contents::find( $id );
$model->delete();
删除(软删除)的模型可以通过 timestamp
进行识别,如果 deleted_at
字段为 NULL
,则表示未被删除,使用 restore
方法会使 deleted_at
字段变为 NULL
。 若要永久删除一个模型,则可以使用 forceDelete
方法。
你实际上执行的是普通删除。但在指定模型时,需要将其设置为软删除模型。
因此,在你的模型上添加以下代码:
class Contents extends Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
然后在您的代码中像平常一样执行删除操作:
$id = Contents::find( $id );
$id ->delete();
请确保您的表中有deleted_at
列。
$user->forceDelete();
。 - majidarifforceDelete()
会从回收站中删除吗? - DolDurmaIlluminate\Database\Eloquent\SoftDeletingTrait;
。 - A F我刚刚使用 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 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'];
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model {
use SoftDeletes;
}
控制器
public function destroy($id)
{
User::find($id)->delete();
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
User::where('email', 'youremail@example.com')->delete();
或者
User::where('email', 'youremail@example.com')->softDeletes();
以下是来自laravel.com的详细信息:
http://laravel.com/docs/eloquent#soft-deleting
软删除模型时,实际上并没有从数据库中删除。相反,记录上设置了一个deleted_at时间戳。要为模型启用软删除,请在模型上指定softDelete属性:
class User extends Eloquent {
protected $softDelete = true;
}
要向您的表中添加一个deleted_at
列,您可以使用迁移中的softDeletes
方法:
$table->softDeletes();
现在,当您调用模型上的删除方法时,deleted_at列将被设置为当前时间戳。当查询使用软删除的模型时,“已删除”的模型将不会包含在查询结果中。
为了启用此功能,我们需要在数据库(或迁移)中添加一个
deleted_at
列,用于映射目的。
例如:
public function up()
{
Schema::create('users', function (Blueprint $table) {
...
$table->softDeletes(); // soft delete
});
}
Illuminate\Database\Eloquent\SoftDeletes
特性。<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
delete()
方法。$user->delete();