【问题标题】:Laravel Soft Delete postsLaravel 软删除帖子
【发布时间】:2014-04-20 23:50:49
【问题描述】:

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

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

$id = Contents::find($id);
$id->softDeletes();

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    更新版本(Version 5.0 & Later):

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    
    class Post extends Model {
    
        use SoftDeletes;
    
        protected $table = 'posts';
    
        // ...
    }
    

    软删除模型时,它实际上并没有从您的 数据库。相反,在记录上设置了deleted_at 时间戳。到 为模型启用软删除,指定 softDelete 属性 模型 (Documentation)。

    对于 (Version 4.2):

    use Illuminate\Database\Eloquent\SoftDeletingTrait; // <-- This is required
    
    class Post extends Eloquent {
    
        use SoftDeletingTrait;
    
        protected $table = 'posts';
    
        // ...
    }
    

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

    例如(使用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 方法。

    【讨论】:

    • 文档已更新,@majidarif 的新答案应被接受为正确
    • 我已经更新了@AakilFernandes,它是在version 4.2 之前给出的,这是正确的,所以我认为我不应该对这个答案投反对票,但无论如何:-)
    • 也许最好先重新排序按最新更新排序的答案部分?似乎有点过时必须通过历史不适用的方式滚动。不过感谢您的回答,这正是我想要的!
    【解决方案2】:

    您实际上执行了正常的删除操作。但在您指定的模型上,它是一个软删除模型。

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

    class Contents extends Eloquent {
    
        use SoftDeletingTrait;
    
        protected $dates = ['deleted_at'];
    
    }
    

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

    $id = Contents::find( $id );
    $id ->delete();
    

    还要确保您的桌子上有 deleted_at 列。

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

    【讨论】:

    • 那么如果在这个模型中如何删除正常的帖子呢?
    • 你可以使用$user-&gt;forceDelete();
    • 我的意思是如何不使用SoftDelete? delete 在正常模式下从数据库中删除。我认为forceDelete()Trash 中删除是吗?
    • Laravel 4.2 使用 Trait 软删除记录。接受的答案不适用于 Laravel 4.2。
    • 确保use Illuminate\Database\Eloquent\SoftDeletingTrait;
    【解决方案3】:

    只是 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'];
    

    【讨论】:

      【解决方案4】:

      在 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();
      }
      

      【讨论】:

        【解决方案5】:

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

        迁移文件中添加:

        $table->softDeletes();
        

        模型中:

        use Illuminate\Database\Eloquent\SoftDeletes;
        
        class User extends Model
        {
            use SoftDeletes;
            ...
        ];
        

        }

        控制器中:

        $user->delete();
        

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

        User::withTrashed()->find($id);->restore();
        

        【讨论】:

          【解决方案6】:

          这是来自 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 列将被设置为当前时间戳。查询使用软删除的模型时,“已删除”的模型不会包含在查询结果中。

          【讨论】:

            【解决方案7】:

            在最新版本的 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”列。

            【讨论】:

              猜你喜欢
              • 2017-10-05
              • 2016-03-05
              • 2020-07-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-11-04
              • 2021-07-09
              • 2013-06-19
              相关资源
              最近更新 更多