zoukankan      html  css  js  c++  java
  • Laravel设置软删除及其恢复系列操作

    软删除及其相关实现

    • 在模型类中要使用SoftDeletestrait并设置$date属性数组
    <?php
    
    namespace AppModels;
    
    use IlluminateDatabaseEloquentModel;
    use IlluminateDatabaseEloquentSoftDeletes;
    
    class Student extends Model
    {
        use SoftDeletes;
    
        //设置表名
        public $table = 'students';
    
        //设置主键
        public $primaryKey = 'id';
    
        protected $dates = ['delete_at'];
    }
    
    • 数据迁移中软删除字段写法
    <?php
    
    use IlluminateDatabaseSchemaBlueprint;
    use IlluminateDatabaseMigrationsMigration;
    
    class AlterStudentsDeletedAt extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('students', function (Blueprint $table) {
                $table->timestamps();
                $table->softDeletes();
            });
        }
    }
    
    • 控制器中使用
    public function destroy(Student $student)
    {
        $student->delete();
        if (!$student->trashed()) {
            return redirect()->back()->with('danger', '学生信息删除失败,学生ID:'.$student->id);
        }
    
        return redirect()->route('students.index')->with('success', '学生信息删除成功,学生ID:'.$student->id);
    }
    
    • 查询结果包含软删除模型
    $students = Student::withTrashed()->get();
    dd($students->toArray());
    
    • 只要查看被软删除的模型
    $students = Student::onlyTrashed()->get();
    dd($students->toArray());
    
    • 软删除恢复
    $student = Student::find(6);
    $student->restore();
    
    • 恢复多个模型
    Student::withTrashed()->where('id','>',1)->restore();
    • 恢复所有模型
    Student::withTrashed()->restore();
    
    • 恢复关联查询模型
    $student = Student::find(6);
    $student->history()->restore();
    
    • 强制删除(丛数据库中删除)
    $student = Student::find(6);
    $student->forceDelete();
    

    链接:https://www.jianshu.com/p/a48fb2b6adfa


  • 相关阅读:
    让requestAnimationFrame实现定时调用功能
    mapbox-gl添加下雪效果
    精细化道路webgl可视化
    https://b.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/15/26919/13562.vector.pbf?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6t
    交叉熵
    K-SVD字典学习及其实现(Python)
    最优方向法(MOD)
    基追踪及其实现
    内点法
    单纯形法MATALAB实现
  • 原文地址:https://www.cnblogs.com/lxwphp/p/10733408.html
Copyright © 2011-2022 走看看