Soft Delete all records from a table in laravel 5

后端 未结 3 1912
迷失自我
迷失自我 2020-12-22 14:29

Is there any way to soft delete all the existing rows in a table? I have tried ( Prospect::delete(); ) it deleted all the rows permanently, But it doesn\'t work in soft dele

相关标签:
3条回答
  • 2020-12-22 14:55

    If you are using Laravel framework older than 4.2, then you can set the soft delete in your model as,

    class ModelName extends Eloquent {
        protected $table = 'table_name';
        protected $softDelete = true;
    }
    

    If you are using Laravel framework 4.2, then you can set the soft delete in your model as,

    use Illuminate\Database\Eloquent\SoftDeletingTrait;
    class ModelName extends Eloquent {
        use SoftDeletingTrait;
        protected $table = 'table_name';
    }
    

    If you are using Laravel framework above 4.2, then you can set the soft delete in your model as,

    use Illuminate\Database\Eloquent\SoftDeletes;
    class ModelName extends Eloquent {
        use SoftDeletes;
        protected $table = 'table_name';
    }
    

    I hope you are using Laravel 5. So you can use the third method.

    0 讨论(0)
  • 2020-12-22 15:09

    It usually not delete rows in database.You should add a column isActive etc. and set it (1/0) or (true/false). if you want show rows,you should "Select * from table Where isActive=1"

    0 讨论(0)
  • 2020-12-22 15:20

    look at this soft delete

    use this trait in your model :

    use Illuminate\Database\Eloquent\SoftDeletes;
    

    and add this to your model and database schema :

    model:

    class Flight extends Model
    {
        use SoftDeletes;
    
        /**
         * The attributes that should be mutated to dates.
         *
         * @var array
         */
        protected $dates = ['deleted_at'];
    }
    

    database schema

    Schema::table('flights', function ($table) {
        $table->softDeletes();
    });
    

    Now, when you call the delete method on the model, the deleted_at column will be set to the current date and time. And, when querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results.

    To determine if a given model instance has been soft deleted, use the trashed method:

    if ($flight->trashed()) {
        //
    }
    
    0 讨论(0)
提交回复
热议问题