提交 5c7b554d 编写于 作者: 彭世瑜's avatar 彭世瑜

fix

上级 c7f5f45a
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
- 链接:https://pan.baidu.com/s/1Y_o3RdUEsrj6tHchMwY3Mg - 链接:https://pan.baidu.com/s/1Y_o3RdUEsrj6tHchMwY3Mg
- 提取码:f4qz - 提取码:f4qz
其他课程:https://www.ycku.com/
配置 Makefile 文件 配置 Makefile 文件
```bash ```bash
...@@ -25,3 +27,10 @@ dev: ...@@ -25,3 +27,10 @@ dev:
[表单伪造和 CSRF 保护](blog/laravel/csrf.md) [表单伪造和 CSRF 保护](blog/laravel/csrf.md)
[数据库](blog/laravel/database.md) [数据库](blog/laravel/database.md)
[构造器的查询](blog/laravel/sql-builder.md)
[构造器的增删改](blog/laravel/sql-builder-modify.md)
[模型的定义](blog/laravel/model.md)
## 模型的定义
创建了一个User.php模型
```bash
php8 artisan make:model User
```
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
// 表名
protected $table = 'user';
// 默认主键
protected $primaryKey = 'id';
// 默认主键自增
public $incrementing = true;
// 默认主键类型为int
protected $keyType = 'int';
// 默认处理created_at和updated_at两个时间戳
public $timestamps = true;
// 自定义时间戳的格式
protected $dateFormat = 'U';
// 创建时间 created_at 字段名
const CREATED_AT = 'create_time';
// 更新时间 updated_at字段名
const UPDATED_AT = 'update_time';
// 默认数据库连接
protected $connection = 'mysql';
}
```
安装代码提示插件
https://github.com/barryvdh/laravel-ide-helper
```bash
composer8 require --dev barryvdh/laravel-ide-helper
# 为 Facades 生成注释 php
php8 artisan ide-helper:generate
# 为数据模型生成注释
php8 artisan ide-helper:models
# 生成 PhpStorm Meta file
php8 artisan ide-helper:meta
```
模型的数据库操作和查询构造器大体相同
```php
// 查询所有记录
User::get();
// select * from `user`
```
https://www.bilibili.com/video/BV1gE411j78F?p=18&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
# 构造器的增删改
## 新增
```php
//新增一条记录
DB::table('user')->insert([
'name' => '曹操',
]);
// insert into `user` (`name`) values (?)
//新增多条记录
DB::table('user')->insert([
['name' => '刘备'],
['name' => '关羽'],
['name' => '张飞']
]);
// insert into `user` (`name`) values (?), (?), (?)
// 忽略重复新增数据的错误
DB::table('user')->insertOrIgnore([
'id' => 1,
'name' => '李白'
]);
// insert ignore into `user` (`id`, `name`) values (?, ?)
//获取新增后返回的 ID
$id = DB::table('user')->insertGetId([
'name' => '李白'
]);
// insert into `user` (`name`) values (?)
```
## 更新
```php
//更新修改一条数据
DB::table('user')->where('id', 10)
->update([
'name' => '李红',
]);
// update `user` set `name` = ? where `id` = ?
//参数 1:修改的条件
//参数 2:修改的内容(新增的内容)
DB::table('user')->updateOrInsert(
['id'=>11],
['name'=>'李黑']
);
// select exists(select * from `user` where (`id` = ?)) as `exists`
// 不存在
// insert into `user` (`id`, `name`) values (?, ?)
// 存在
// update `user` set `name` = ? where (`id` = ?) limit 1
//新增时,转换为 json 数据
DB::table('user')->insert([
'name' => json_encode(['age'=>19, 'name'=>'孙权'], JSON_UNESCAPED_UNICODE)
]);
//修改时,使用 object->id 指定
DB::table('user')->where('id', 13)
->update([
'name->age' => 20
]);
// update `user` set `name` = json_set(`name`, '$."age"', ?) where `id` = ?
//默认自增/自减为 1,可设置
DB::table('user')->where('id', 1)->increment('age');
// update `user` set `age` = `age` + 1 where `id` = ?
DB::table('user')->where('id', 2)->decrement('age', 2);
// update `user` set `age` = `age` - 2 where `id` = ?
```
## 删除
```php
// 删除一条数据
DB::table('user')->delete(1);
// delete from `user` where `user`.`id` = ?
DB::table('user')->where('id', 2)->delete();
// delete from `user` where `id` = ?
// 清空
DB::table('user')->delete();
// delete from `user`
DB::table('user')->truncate();
// truncate table `user`
```
\ No newline at end of file
# 构造器的查询
打印执行SQL
```php
DB::listen(function($query) {
$sql = $query->sql;
$bindings = $query->bindings;
Log::info('sql: ' . $sql);
Log::info('params: ' . json_encode($bindings, JSON_UNESCAPED_UNICODE));
});
```
查询示例
```php
//获取全部结果
DB::table('user')->get();
// select * from `user`
//获取第一条数据
DB::table('user')->first();
// select * from `user` limit 1
//获取第一条数据的 email 字段值
DB::table('user')->value('name');
// select `name` from `user` limit 1
//通过 id 获取指定一条数据
DB::table('user')->find(20);
// select * from `user` where `id` = ? limit 1
//获取单列值的集合
DB::table('user')->pluck('name');
// select `name` from `user`
//获取单列值的集合(value, key)
DB::table('user')->pluck('name', 'id');
// select `name`, `id` from `user`
```
分块
```php
//切割分块执行,每次读取 3 条,id 排序;
DB::table('user')->orderBy('id')->chunk(3, function ($users) {
foreach ($users as $user) {
echo $user->name;
}
});
// select * from `user` order by `id` asc limit 3 offset 0
// select * from `user` order by `id` asc limit 3 offset 3
```
聚合查询
```php
DB::table('user')->count();
// select count(*) as aggregate from `user`
DB::table('user')->max('id');
// select max(`id`) as aggregate from `user`
DB::table('user')->min('id');
// select min(`id`) as aggregate from `user`
DB::table('user')->avg('id');
// select avg(`id`) as aggregate from `user`
DB::table('user')->sum('id');
// select sum(`id`) as aggregate from `user`
```
判断是否存在
```php
DB::table('user')->where('id', 1)->exists();
// select exists(select * from `user` where `id` = ?) as `exists`
DB::table('user')->where('id', 18)->doesntExist();
// select exists(select * from `user` where `id` = ?) as `exists`
```
## 查询表达式
1、select 查询
```php
// 设置显示的列,设置列别名
DB::table('user')->select('name as username', 'id')->get();
// select `name` as `username`, `id` from `user`
// 给已经构建好的查询添加更多字段
$query = DB::table('user')->select('name as username', 'id');
$query->addSelect('create_time')->get();
// select `name` as `username`, `id`, `create_time` from `user`
// 结合原生 SQL 实现复杂查询
DB::table('user')->select(DB::raw('COUNT(*) AS count, name'))
->groupBy('name')
->get();
// select COUNT(*) AS count, name from `user` group by `name`
// 或者直接使用 selectRaw()方法实现原生
DB::table('user')->selectRaw('COUNT(*) AS count, name')
->groupBy('name')
->get();
// select COUNT(*) AS count, name from `user` group by `name`
//使用 havingRaw 方法实现分组筛选
DB::table('user')->selectRaw('COUNT(*) AS count, name')
->groupBy('name')
->havingRaw('count>1')
->get();
// select COUNT(*) AS count, name from `user` group by `name` having count>1
```
## where 查询
```php
// where 查询完整形式
DB::table('user')->where('id', '=', 19)->get();
// select * from `user` where `id` = ?
// 可以省略掉=号参数
DB::table('user')->where('id', 19)->get();
// select * from `user` where `id` = ?
DB::table('user')->where('id', '>=', 3)->get();
// select * from `user` where `id` >= ?
DB::table('user')->where('name', 'like', '%小%')->get();
// select * from `user` where `name` like ?
// 用数组来分别添加条件
// 查看 SQL 语句用->toSql()替换->get()
DB::table('user')->where([
'id' => 90,
'name' => 'Tom'
])->get();
// select * from `user` where (`id` = ? and `name` = ?)
// 如果条件非等于
DB::table('user')->where([
['id', '>=', 90],
['name', '=', 'Tom'] ]
)->get();
// select * from `user` where (`id` >= ? and `name` = ?)
```
## where 派生查询
```php
// where() + orWhere实现or条件查询
DB::table('user')->where('id', '>', 5)
->orWhere('name', 'Tom')
->get();
// select * from `user` where `id` > ? or `name` = ?
//orWhere()结合闭包查询
$users = DB::table('users')
->where('price', '>', '95')->orWhere(function ($query) {
$query->where('gender', '女')
->where('username','like','%小%');
})->toSql();
// select * from `users` where `price` > ? or (`gender` = ? and `username` like ?)
// whereBetween 查询区间价格 60~90 之间
$users = DB::table('users')->whereBetween('price', [60, 90])->toSql();
// select * from `users` where `price` between ? and ?
// PS:这里还支持相关三种:
// whereNotBetween/orWhereBetween/orWhereNotBetween;
// whereIn 查询数组里匹配的数值
$users = DB::table('users')->whereIn('id', [20,30,50])->toSql();
// select * from `users` where `id` in (?, ?, ?)
// PS:这里还支持相关三种:
// whereNotIn/orWhereIn/orWhereNotIn;
//whereNull 查询字段值为 Null 的记录
$users = DB::table('users')->whereNull('uid')->toSql();
// select * from `users` where `uid` is null
// PS:这里还支持相关三种:
// whereNotNull/orWhereNull/orWhereNotNull;
// whereYear 查询指定日期的记录,或大于
$users = DB::table('users')->whereDate('create_time', '2018-12-11')->toSql();
// select * from `users` where date(`create_time`) = ?
// PS:这里还支持相关四种:
// whereYear/whereMonth/whereDay/whereTime,支持 or 前缀;
// PS:三个参数支持大于小于之类的操作 orWhereDate('create_time','>', '2018-12-11')
```
## 排序分组
```php
//判断两个相等的字段,同样支持 orWhereColumn() //支持符号'create_time','>', 'update_time' //支持符号支持数组多个字段格式['create_time','>', 'update_time']
DB::table('user')
->whereColumn('create_time', 'update_time')
->get();
// select * from `user` where `create_time` = `update_time`
//支持 orderByRaw 和 orderByDesc 倒序方法
DB::table('user')
->orderBy('id', 'desc')
->get();
// select * from `user` order by `id` desc
// 按照创建时间倒序排,默认字段 created_at
DB::table('user')->latest('create_time')->toSql();
// select * from `user` order by `create_time` desc
//随机排序
DB::table('user')->inRandomOrder()->get();
// select * from `user` order by RAND()
//从第 3 条开始,显示 3 条
DB::table('user')->skip(2)->take(3)->toSql();
// select * from `user` limit 3 offset 2
DB::table('user')->offset(2)->limit(3)->get();
// select * from `user` limit 3 offset 2
//when 实现条件选择
DB::table('user')->when(true,
function ($query) {
$query->where('id', 19);
},
function ($query) {
$query->where('name', 'Tom');
}
)->get();
// true: select * from `user` where `id` = ?
// false: select * from `user` where `name` = ?
// 如果MySQL在5.7+,有支持JSON数据的新特性;
DB::table('user')->where('list->id', 19)->first();
// select * from `user` where json_unquote(json_extract(`list`, '$."id"')) = 19 limit 1
```
## 子查询
```php
//通过 books 表数据,查询到 users 表关联的所有用户
DB::table('users')->whereExists(function ($query) {
$query->selectRaw(1) ->from('books')
->whereRaw('laravel_books.user_id = laravel_users.id');
})->toSql();
// select * from `users` where exists (select 1 from `books` where laravel_books.user_id = laravel_users.id)
//whereRaw 这句也可以替代为:whereColumn('books.user_id','users.id');
// PS:select 1 from,一般用于子查询的手段,目的是减少开销,提升效率
//id=子查询返回的 user_id
DB::table('users')->where('id', function ($query) {
$query->select('user_id')
->from('books')
->whereColumn('books.user_id','users.id');
})->toSql();
// select * from `users` where `id` = (select `user_id` from `books` where `books`.`user_id` = `users`.`id`)
```
## join 查询
```php
// join实现内联接的多表查询
$sql = DB::table('users')
->join('books', 'users.id', '=', 'books.user_id')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->select('users.id', 'users.username', 'users.email','books.title', 'profiles.hobby')
->toSql();
// select `users`.`id`, `users`.`username`, `users`.`email`, `books`.`title`, `profiles`.`hobby`
// from `users` inner join `books` on `users`.`id` = `books`.`user_id`
// inner join `profiles` on `users`.`id` = `profiles`.`user_id`
// 使用leftjoin左连接或rightjoin右连接
$sql = DB::table('users')
->leftJoin('books', 'users.id', '=', 'books.user_id')
->rightjoin('profiles', 'users.id', '=', 'profiles.user_id')
->toSql();
// select * from `users`
// left join `books` on `users`.`id` = `books`.`user_id`
// right join `profiles` on `users`.`id` = `profiles`.`user_id`
// crossjoin交叉连接查询,会生成笛卡尔积,再用distinct()取消重复
DB::table('users') ->crossJoin('books')
->select('username', 'email')
->distinct()
->toSql();
// select distinct `username`, `email`
// from `users` cross join `books`
// 实现闭包查询,和where类似,只不过要用on和orOn方法
DB::table('users') ->join('books', function ($join) {
//支持 orOn 连缀
$join->on('users.id', '=', 'books.user_id');
})->toSql();
// select * from `users`
// inner join `books` on `users`.`id` = `books`.`user_id`
// joinSub实现子连接查询
$query = DB::table('books')->selectRaw('user_id,title');
$users = DB::table('users')->joinSub($query,'books', function ($join) {
$join->on('users.id', '=', 'books.user_id');
})->toSql();
// select * from `users`
// inner join (select user_id,title from `books`) as `books`
// on `users`.`id` = `books`.`user_id`
// union()或unionAll()方法实现两个查询的合并操作
//union 取消重复,unionAll 不取消重复
$query = DB::table('users');
$users = DB::table('users')
->union($query)
->get();
// (select * from `users`) union (select * from `users`)
```
...@@ -5,19 +5,29 @@ ...@@ -5,19 +5,29 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Coding Tree</title> <title>Coding Tree</title>
<meta http-equiv="X-UA-Compatible" <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
content="IE=edge,chrome=1" /> <meta name="description" content="编程学习路上的笔记与知识整理收集">
<meta name="description"
content="编程学习路上的笔记与知识整理收集">
<meta name="viewport" <meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="icon" <link rel="icon" href="img/logo-icon.png">
href="img/logo-icon.png">
<link rel="stylesheet" <link rel="stylesheet" href="static/js/vue@4.12.2.min.css">
href="static/js/vue@4.12.2.min.css">
<style> <style>
/* 文字大小 */
body {
font-size: 16px;
}
/* 代码区域文字大小 */
.markdown-section>:not(h1):not(h2):not(h3):not(h4):not(h5):not(h6) code {
font-size: 0.9rem;
}
.sidebar ul li a {
font-size: 16px;
}
:root { :root {
--toc-width: 300px; --toc-width: 300px;
} }
...@@ -63,8 +73,6 @@ ...@@ -63,8 +73,6 @@
.page_toc:hover::-webkit-scrollbar-track { .page_toc:hover::-webkit-scrollbar-track {
background: hsla(0, 0%, 53.3%, .1) background: hsla(0, 0%, 53.3%, .1)
} }
</style> </style>
</head> </head>
...@@ -128,8 +136,7 @@ ...@@ -128,8 +136,7 @@
<!-- 下载地址 --> <!-- 下载地址 -->
<!-- https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-python.min.js --> <!-- https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-python.min.js -->
<link rel="stylesheet" <link rel="stylesheet" href="static/js/toc.css">
href="static/js/toc.css">
<script src="static/js/toc.js"></script> <script src="static/js/toc.js"></script>
</body> </body>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册