【问题标题】:Laravel 4.2 Soft Delete not workingLaravel 4.2 软删除不起作用
【发布时间】:2014-10-19 11:57:24
【问题描述】:

我使用 laravel 4.2.8 和 Eloquent ORM。 当我尝试软删除时,它不起作用。它从我的数据库中删除数据。 我想在逻辑上而不是物理上删除数据。 在这里,我给出了我尝试过的代码

型号

use Illuminate\Auth\UserInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class User extends Eloquent implements UserInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
        public $timestamps = true;
        protected $softDelete = true;
        protected $dates = ['deleted_at'];

        public static function boot()
        {
            parent::boot();
            static::creating(function($post)
            {
                $post->created_by = Auth::user()->id;
                $post->updated_by = Auth::user()->id;
            });

            static::updating(function($post)
            {
                $post->updated_by = Auth::user()->id;
            });

            static::deleting(function($post)
            {
                $post->deleted_by = Auth::user()->id;
            });
        }
}

控制器

public function destroy($id) {
        // delete
        $user = User::find($id);
        $user->delete();

        // redirect
        return Redirect::to('admin/user');
    }

【问题讨论】:

    标签: php laravel laravel-4 frameworks


    【解决方案1】:

    你需要像这样使用 trait;

    use SoftDeletingTrait;
    

    在课程开始时。

    【讨论】:

      【解决方案2】:

      从 4.2 开始,您现在需要 use SoftDeletingTrait;,不再设置 protected $softDelete = true;

      use Illuminate\Auth\UserInterface;
      use Illuminate\Database\Eloquent\SoftDeletingTrait;
      
      class User extends Eloquent implements UserInterface {
      
          use SoftDeletingTrait;
      
          protected $table = 'users';
          public $timestamps = true;
          protected $dates = ['deleted_at'];
      
          public static function boot()
          {
              parent::boot();
              static::creating(function($post)
              {
                  $post->created_by = Auth::user()->id;
                  $post->updated_by = Auth::user()->id;
              });
      
              static::updating(function($post)
              {
                  $post->updated_by = Auth::user()->id;
              });
      
              static::deleting(function($post)
              {
                  $post->deleted_by = Auth::user()->id;
              });
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 2017-10-16
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多