Laravel设置软删除及其恢复系列操作
软删除及其相关实现
- 在模型类中要使用SoftDeletestrait并设置$date属性数组
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; class Student extends Model
{
use SoftDeletes; //设置表名
public $table = 'students'; //设置主键
public $primaryKey = 'id'; protected $dates = ['delete_at'];
}
- 数据迁移中软删除字段写法
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; 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','>',)->restore();
- 恢复所有模型
Student::withTrashed()->restore();
- 恢复关联查询模型
$student = Student::find(6);
$student->history()->restore();
- 强制删除(丛数据库中删除)
$student = Student::find(6);
$student->forceDelete();
链接:https://www.jianshu.com/p/a48fb2b6adfa
Laravel设置软删除及其恢复系列操作的更多相关文章
- 关于Domino数据库的软删除
在Domino的数据库属性的 “高级” 附签(选择文件->数据库->属性),选中“允许软删除”,这样我们就启用了软删除功能,当一个文档没有删除的时候我们可以使用NotesDatabase的 ...
- php手记之05-tp5软删除
01-需要在设置软删除的模型里设置
- tp 5 框架 ajax软删除,回收站,数据恢复
//HTML代码: <td> <span onclick="del({$v.id})">删除</span> </td> //ajax ...
- DDD Code First 迁移数据实现EF CORE的软删除,值对象迁移配置
感谢Jeffcky大佬的博客: EntityFramework Core 2.0全局过滤 (HasQueryFilter) https://www.cnblogs.com/CreateMyself/p ...
- laravel and lumen 软删除操作
知识都是有联系的,这绝对是真理.作为一名小白,看了一点官方文档,把我自己理解的软删除操作给大家讲讲.有些就是套用官方文档的话. 定义:什么是软删除呢,所谓软删除指的是数据表记录并未真的从数据库删除,而 ...
- laravel框架总结(九) -- 软删除
当模型被软删除时,它们并不会真的从数据库中被移除.而是会在模型上设置一个 deleted_at 属性并将其添加到数据库.如果对应模型被软删除,则deleted_at字段的值为删除时间,否则该值为空. ...
- Laravel 查询包括软删除的记录
查询结果包括已被软删除的记录: Model::withTrashed()->get(); 只查询软删除记录: Model::onlyTrashed()->get(); PS:个人博客-La ...
- Windows下环境变量显示、设置或删除操作详情
显示.设置或删除 cmd.exe 环境变量. SET [variable=[string]] variable 指定环境变量名. string 指定要指派给变量的一系列字符串. 要显示当前环境变量,键 ...
- laravel文件存储、删除、移动等操作
laravel文件存储.删除.移动等操作 一.总结 一句话总结: 启示:可以在操作遇到问题的时候,找文档找实例好好实验一下,也就是学习巩固一下,不必一定要死纠排错 1.laravel文件删除注意? 1 ...
随机推荐
- 【ZJOI2016】线段树
[ZJOI2016]线段树 ZJOI的题神啊. 我们考虑计算每个位置\(p\),它在操作过后变成第\(x\)个数的操作序列数. 我们枚举\(x\).我们先得到了\(L_x,R_x\)表示最左边比\(x ...
- UVA1609-Foul Play(构造+递归)
Problem UVA1609-Foul Play Accept: 101 Submit: 514Time Limit: 3000 mSec Problem Description Input Fo ...
- Python:Day43 抽屉
1.关于inline-block和float的理解 inline-block和float都可以实现块级标签放在同一行上,inline不好设置左右对齐,只能通过margin和padding调节.而flo ...
- Python:Day41 http、css
HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. 2.请求协议 请求协议的格式如下: 请求首行: // ...
- 【vue】vue +element 搭建项目,将js函数变成vue的函数
demo:时间转换 1.目录 <1>在src文件夹下新建文件夹prototypefns--------在此文件夹创建util.js, <2>在prototypefns下新建文件 ...
- JSoup抓取本地页面
File in = new File("C:/Users/li/Desktop/2.html"); Document doc01 = Jsoup.parse(in, "U ...
- day25 Python四个可以实现自省的函数,反射
python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 四个可以实现自省的函数 下列方法适用于类和对象(一切皆对象,类本身也是一个对象) ...
- Spring Security(十一):4. Samples and Guides (Start Here)
If you are looking to get started with Spring Security, the best place to start is our Sample Applic ...
- Edusoho之LAMP环境搭建
主要参考官方文档Ubuntu16.04+Apache+PHP+MySQL+EduSoho 安装教程LAMP环境按照如下搭建是没有问题的,本地虚拟机试验是完全没有问题的. 1.更新 sudo apt-g ...
- 在Java中,将ExecutorService转为守护程序
问题描述 我正在Java 1.6中使用一个ExecutoreService,简单地开始 ExecutorService pool = Executors.newFixedThreadPool(THRE ...