“bde4471c4e93b642cddf0f29880e866d11e04605”上不存在“examples/Cpp/fit_a_line/test_server.py”
提交 569a0398 编写于 作者: 彭世瑜's avatar 彭世瑜

fix

上级 213bdb5e
# 集合 collection
文档:https://learnku.com/docs/laravel/9.x/collections/12225
```php
//使用 dd 查看它的类型
dd($collection);
// 自定义集合方法
$collection = collect(['Tom', 'Jack', '王五', null]);
Collection::macro('toUpper', function () {
return $this->map(function ($value) {
return strtoupper($value);
});
});
return $collection->toUpper();
// ["TOM", "JACK","王五",""]
```
## 创建集合
### collect
```php
// 创建一个数据集合
$collection = collect(['张三', '李四', '王五', null]);
```
### pad
```php
// 将用给定的值填充数组
collect()->pad(5, 0);
// [0, 0, 0, 0, 0 ]
```
### range
```php
// 返回一个包含指定范围之间整数的集合
collect()->range(0, 5);
// 或者
Collection::range(0, 5);
// [0, 1, 2, 3, 4, 5 ]
```
### times
```php
// 通过调用给定次数的回调函数来创建新集合
Collection::times(3, function ($number) {
return $number * 2;
});
// [2, 4, 6 ]
```
## 查询集合
### all
```php
// 返回由集合表示的底层数组
$collection = collect([1, 2, 3, 3, 4]);
// 全部元素
$collection->all();
// [1,2,3,3,4]
```
### first
```php
// 返回判断成立的第一条数值
$collection = collect([1, 2, 3, 4]);
return $collection->first(function ($value) {
return $value > 2;
}
);
// 3
// 相关的还有 every()、except()、only()、firstWhere()、last()等方法;
```
### get
```php
// 通过键名找值
$collection = collect([
'name'=>'Tom',
'age'=> 23
]);
return $collection->get('name');
// Tom
// 还有 pluck()等;
```
### has
```php
// 判断集合中是否存在指定键
$collection = collect([
'name'=>'Tom',
'age'=> 23
]);
return $collection->has('name');
// 1
```
### search
```php
// 查找
$collection = collect(['Tom', '李四', '王五', null]);
// 查找 返回 key,找不到返回 false
$collection->search("李四");
// 1
```
### random
```php
// 从集合中返回一个随机项
$collection = collect([1, 2, 3, 4, 5]);
return $collection->random();
// 3
```
### where
```php
$collection = collect([
['name'=>'Mr.Lee', 'gender'=>'男'],
['name'=>'Miss.Zhang', 'gender'=>'女']
]);
return $collection->where('name', 'Mr.Lee');
// [
// {
// "name": "Mr.Lee",
// "gender": "男"
// }
// ]
```
### values
```php
// 返回键被重置为连续编号的新集合
$collection = collect([
3 => 1,
4 => 2
]);
$collection->values();
// [1, 2 ]
```
### keys
```php
// 返回集合的所有键
$collection = collect([
3 => 1,
4 => 2
]);
$collection->keys();
// [3, 4 ]
```
## 集合修改
### pop
```php
// 移出集合中最后一个值
$collection = collect([1, 2, 3, 4, 5]);
$collection->pop();
return $collection;
// [1, 2, 3, 4 ]
// 还有 pull()、push()、put()方法
```
### slice
```php
// 切片
$collection = collect([1, 2, 3, 4, 5]);
$result = $collection->slice(3);
// "result":
// {
// "3": 4,
// "4": 5
// }
// "collection":
// [1, 2, 3, 4, 5 ]
```
### splice
```php
// 删除数组中指定索引的元素
$collection = collect([1, 2, 3, 4, 5]);
$result = $collection->splice(3);
// "result":
// [4, 5 ]
// "collection":
// [1, 2, 3 ]
```
### shift
```php
// 从集合中移除并返回第一项
$collection = collect([1, 2, 3, 4, 5]);
$collection->shift();
return $collection;
// [2, 3, 4, 5 ]
```
## 集合转换
### flatten
```php
// 多维数组转换为一维
$collection = collect([1, 2, [3, 4]]);
$collection->flatten();
// [ 1, 2, 3, 4]
```
### chunk
```php
// 集合的分割,这里好像有bug
$collection->chunk(2);
// [
// ["张三", "李四" ],
// { "2": "王五", "3": null}
// ]
// 解决:重新获取值,舍弃原有的key
$collection->chunk(2)
->map(function (Collection $value) {
return $value->values();
});
// [
// ["Tom", "Jack"],
// ["王五", null]
// ]
```
### merge
```php
// 集合合并
$collection = collect([
'name' => 'Tom',
'age' => 23
]);
$merged = $collection->merge([
'name' => 'Jack',
'school' => 'puk'
]);
return $merged;
// {
// "name": "Jack",
// "age": 23,
// "school": "puk"
// }
```
### shuffle
```php
// 随机打乱集合中的项目
$collection = collect([1, 2, 3, 4, 5]);
$collection->shuffle();
// [5, 1, 2, 4, 3 ]
```
### filter
```php
// 过滤
$collection->filter(function ($item) {
return $item;
});
// ["张三", "李四", "王五"]
```
### map
```php
// 映射
$collection->map(function ($value, $key) {
return '[' . $value . ']';
});
// ["[张三]", "[李四]", "[王五]", "[]"]
```
### reduce
```php
// 求集合的乘积
$collection = collect([1, 2, 3, 4, 5]);
return $collection->reduce(function ($preValue, $value){
return $preValue * $value;
}, 1);
// 120
```
### sort
```php
// 排序
$collection = collect([3, 1 , 5, 2, 7]);
return $collection->sort()->values();
// [1, 2, 3, 5, 7 ]
// sortBy()、sortByDesc()、sortKeys()等
```
### reverse
```php
// 反转集合项的顺序,保留原始键
$collection = collect(['a', 'b', 'c', 'd', 'e']);
$reversed = $collection->reverse();
return $reversed;
// {
// "0": "a",
// "1": "b",
// "2": "c",
// "3": "d",
// "4": "e"
// }
```
### duplicates
```php
// 重复的值
$collection = collect([1, 2, 3, 3, 4]);
$collection->duplicates();
// {
// "3": 3
// }
// 严格派生方法:duplicatesStrict()
```
### unique
```php
// 返回集合中所有唯一项。返回的集合保留着原数组的键
$collection = collect([1, 1, 2, 2, 3, 4, 2]);
$collection->unique();
// {
// "0": 1,
// "2": 2,
// "4": 3,
// "5": 4
// }
```
### toJson
```php
// 将集合转换成 JSON 字符串
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toJson();
// {
// "name": "Desk",
// "price": 200
// }
```
### toArray
```php
// 将集合转换成 PHP array
$collection = collect(['name' => 'Desk', 'price' => 200]);
$collection->toArray();
// {
// "name": "Desk",
// "price": 200
// }
```
## 分组聚合
### avg
```php
// 平均值
$collection = collect([1, 2, 3, 3, 4]);
$collection->avg();
// 2.6
$collection = collect([
['男' => 1],
['女' => 1],
['男' => 3]
]);
$collection->avg('男');
// 2
```
### count
```php
// 统计总数
$collection = collect([1, 2, 3, 3, 4]);
$collection->count();
// 5
// 相关的还有 sum()、min()、max()等
```
### countBy
```php
// 分组统计
$collection = collect([1, 2, 3, 3, 4]);
$collection->countBy();
// {
// "1": 1,
// "2": 1,
// "3": 2,
// "4": 1
// }
// 分类统计邮箱个数
$collection = collect([
'xiaoxin@163.com',
'yihu@163.com',
'xiaoying@qq.com'
]);
$collection->countBy(function ($value) {
return substr(strrchr($value, '@'), 1);
});
// {
// "163.com": 2,
// "qq.com": 1
// }
```
### groupBy
```php
// 根据指定键对集合项进行分组
$collection = collect([
['name' => 'Tom', 'age' => '23'],
['name' => 'Jack', 'age' => '24'],
['name' => 'Steve', 'age' => '23'],
]);
$grouped = $collection->groupBy('age');
// {
// "23": [
// {
// "name": "Tom",
// "age": "23"
// },
// {
// "name": "Steve",
// "age": "23"
// }
// ],
// "24": [
// {
// "name": "Jack",
// "age": "24"
// }
// ]
// }
```
### diff
```php
// 不相同的部分
$collection = collect([1, 2, 3, 3, 4]);
$collection->diff([1, 3]);
// {
// "1": 2,
// "4": 4
// }
// 还有 diffAssoc()、diffKeys()派生方法;
```
### join
```php
// 将集合的值与字符串连接起来
$collection = collect([1, 2, 3, 4, 5]);
$collection->join();
// 1-2-3-4-5
```
# 数据库 # 数据库 Database
操作数据库的方式: 操作数据库的方式:
......
...@@ -26,11 +26,17 @@ dev: ...@@ -26,11 +26,17 @@ dev:
[表单伪造和 CSRF 保护](blog/laravel/csrf.md) [表单伪造和 CSRF 保护](blog/laravel/csrf.md)
[数据库](blog/laravel/database.md) [数据库 Database](blog/laravel/database.md)
[构造器的查询](blog/laravel/sql-builder.md) [构造器的查询](blog/laravel/sql-builder.md)
[构造器的增删改](blog/laravel/sql-builder-modify.md) [构造器的增删改](blog/laravel/sql-builder-modify.md)
[Model 模型](blog/laravel/model.md) [模型 Model](blog/laravel/model.md)
[集合 collection](/blog/laravel/collection.md)
[模型的数据集合 ](/blog/laravel/model-collection.md)
[模型关联 relation ](/blog/laravel/model-relation.md)
# 模型的数据集合
```php
$users = User::get();
//使用集合方法 map 可以对输出的字段进行转换
$list = $users->map(function ($user) {
$user->name = "[{$user->name}]";
return $user;
});
return $list;
```
常用的集合方法
```php
//判断集合中是否包含指定的模型实例
$users->contains(19);
$users->contains(User::find(19)); //返回不在集合中的所有模型
$users->diff(User::whereIn('id', [19,20,21])->get()); //返回给定主键外的所有模型
$users->except([19,20,21]);
//集合也有 find 方法
$users->find(19);
//返回集合的数量
$users->count();
//返回所有模型的主键
$users->modelKeys();
//返回主键的所有模型
$users->only([19,20,21]); //返回集合中的唯一模型
$users->unique();
```
https://www.bilibili.com/video/BV1gE411j78F?p=25&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
# 模型关联 relation
## 模型的一对一关联
创建数据库表
```sql
-- 用户表
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int NOT NULL DEFAULT '0',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`delete_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='用户表';
-- 用户信息表
CREATE TABLE `profile` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`hobby` varchar(255) NOT NULL DEFAULT '',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`delete_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='用户信息表';
```
初始化数据
```sql
-- 用户表
INSERT INTO `user`(`id`, `name`, `age`, `create_time`, `update_time`, `delete_time`) VALUES (1, '曹真', 23, '2022-07-05 10:32:48', '2022-07-05 10:47:36', NULL);
INSERT INTO `user`(`id`, `name`, `age`, `create_time`, `update_time`, `delete_time`) VALUES (2, '曹丕', 21, '2022-07-05 10:36:47', '2022-07-05 10:47:22', NULL);
-- 用户信息表
INSERT INTO `profile`(`id`, `user_id`, `hobby`, `create_time`, `update_time`, `delete_time`) VALUES (1, 1, '羽毛球', '2022-07-10 22:26:00', '2022-07-10 22:27:07', NULL);
INSERT INTO `profile`(`id`, `user_id`, `hobby`, `create_time`, `update_time`, `delete_time`) VALUES (2, 2, '乒乓球', '2022-07-10 22:27:16', '2022-07-10 22:27:16', NULL);
```
创建Model
```bash
# 用户表
php8 artisan make:model User
# 用户信息表
php8 artisan make:model Profile
# 添加代码提示
php8 artisan ide-helper:models
```
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\User
*
* @property int $id
* @property string $name
* @property int $age
* @property string $create_time
* @property string $update_time
* @property string|null $delete_time
* @property Profile|null $profile
* @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|User query()
* @method static \Illuminate\Database\Eloquent\Builder|User whereAge($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereCreateTime($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereDeleteTime($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereUpdateTime($value)
* @mixin \Eloquent
*/
class User extends Model
{
protected $table = 'user';
// 一对一关联 Profile 表
public function profile()
{
return $this->hasOne(Profile::class, 'user_id', 'id');
}
}
```
```php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Profile
*
* @property int $id
* @property int $user_id
* @property int $hobby
* @property string $create_time
* @property string $update_time
* @property string|null $delete_time
* @property User|null $user
* @method static \Illuminate\Database\Eloquent\Builder|Profile newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Profile newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Profile query()
* @method static \Illuminate\Database\Eloquent\Builder|Profile whereCreateTime($value)
* @method static \Illuminate\Database\Eloquent\Builder|Profile whereDeleteTime($value)
* @method static \Illuminate\Database\Eloquent\Builder|Profile whereHobby($value)
* @method static \Illuminate\Database\Eloquent\Builder|Profile whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Profile whereUpdateTime($value)
* @method static \Illuminate\Database\Eloquent\Builder|Profile whereUserId($value)
* @mixin \Eloquent
*/
class Profile extends Model
{
protected $table = 'profile';
// 反向关联
public function user(){
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
```
```php
User::find(1)->profile;
{
"id": 1,
"user_id": 1,
"hobby": "羽毛球",
"create_time": "2022-07-10 22:26:00",
"update_time": "2022-07-10 22:27:07",
"delete_time": null
}
```
```php
Profile::find(1)->user;
{
"id": 1,
"name": "曹真",
"age": 23,
"create_time": "2022-07-05 10:32:48",
"update_time": "2022-07-05 10:47:36",
"delete_time": null
}
```
## 模型的一对多关联
定义数据表
```sql
-- 用户书单表
CREATE TABLE `book` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`delete_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='用户书单表';
-- 用户书单表
INSERT INTO `book`(`id`, `user_id`, `title`, `create_time`, `update_time`, `delete_time`) VALUES (1, 1, '《三国演义》', '2022-07-10 22:26:00', '2022-07-10 22:27:07', NULL);
INSERT INTO `book`(`id`, `user_id`, `title`, `create_time`, `update_time`, `delete_time`) VALUES (2, 1, '《红楼梦》', '2022-07-10 22:27:16', '2022-07-10 22:27:16', NULL);
```
初始化数据
```bash
# 用户书单表
php8 artisan make:model Book
# 添加代码提示
php8 artisan ide-helper:models
```
定义模型类
```php
class User extends Model
{
protected $table = 'user';
// 一对多关联 Book 表
public function books()
{
return $this->hasMany(Book::class, 'user_id', 'id');
}
}
class Book extends Model
{
protected $table = 'book';
// 反向关联
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
```
使用关联查询
```php
User::find(1)->books;
[
{
"id": 1,
"user_id": 1,
"title": "《三国演义》",
"create_time": "2022-07-10 22:26:00",
"update_time": "2022-07-10 22:27:07",
"delete_time": null
},
{
"id": 2,
"user_id": 1,
"title": "《红楼梦》",
"create_time": "2022-07-10 22:27:16",
"update_time": "2022-07-10 22:27:16",
"delete_time": null
}
]
```
```php
Book::find(1)->user;
{
"id": 1,
"name": "曹真",
"age": 23,
"create_time": "2022-07-05 10:32:48",
"update_time": "2022-07-05 10:47:36",
"delete_time": null
}
```
使用条件查询
```php
User::find(1)
->books()
->where('id', '>', 1)
->get();
[
{
"id": 2,
"user_id": 1,
"title": "《红楼梦》",
"create_time": "2022-07-10 22:27:16",
"update_time": "2022-07-10 22:27:16",
"delete_time": null
}
]
```
https://www.bilibili.com/video/BV1gE411j78F?p=27&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
...@@ -344,7 +344,72 @@ User::withoutGlobalScope(StatusScope::class)->get(); ...@@ -344,7 +344,72 @@ User::withoutGlobalScope(StatusScope::class)->get();
// 取消名称为 status 的全局 // 取消名称为 status 的全局
User::withoutGlobalScope('status')->get(); User::withoutGlobalScope('status')->get();
// select * from `user` // select * from `user`
// 取消全部全局作用域
User::withoutGlobalScopes()->get();
// 取消部分作用域
User::withoutGlobalScopes([
FirstScope::class,
SecondScope::class
])->get();
```
## 模型的访问器和修改器
1、访问器
获取数据时,拦截属性并对属性进行修改
```php
// 前固定 get,后固定 Attribute,Name 是字段名
// 参数 $value 是源字段值,可修改返回
// 属性:name
// name 曹真 -> 【曹真】
public function getNameAttribute($value)
{
return '【'.$value.'】';
}
```
可以创建一个虚拟字段,用已有的数据字段进行整合,不过要进行数据追加
```php
// 将虚拟字段追加到数据对象列表里去
protected $appends = ['info']
// 虚拟字段 info
public function getInfoAttribute(){
// "info": "【曹真】-23"
return "{$this->name}-{$this->age}";
// 使用源字段进行创建虚拟字段 "info": "曹真-23"
return $this->attributes['name'] . '-' . $this->attributes['age'];
}
```
2、修改器
在写入的时候拦截,进行修改再写入
```php
// 修改器,写入数据时,将年龄+10
public function setAgeAttribute($value) {
$this->attributes['age'] = $value + 10;
}
```
```php
// 设置可以自动写入日期的列
// 默认 created_at 和 updated_at
protected $dates = [
'details'
];
// 设置字段输出的类型,比如设置一个布尔型,输出时就是true和false;
// 设置字段类型
protected $casts = [
'details' => 'boolean'
];
``` ```
https://www.bilibili.com/video/BV1gE411j78F?p=19&spm_id_from=pageDriver&vd_source=efbb4dc944fa761b6e016ce2ca5933da
\ No newline at end of file
...@@ -13,6 +13,9 @@ vue.js: 渐进式 JavaScript 框架 ...@@ -13,6 +13,9 @@ vue.js: 渐进式 JavaScript 框架
- [vColorPicker](http://vue-color-picker.rxshc.com/): 基于 Vue 的颜色选择器插件 - [vColorPicker](http://vue-color-picker.rxshc.com/): 基于 Vue 的颜色选择器插件
- [vuedraggable](https://www.npmjs.com/package/vuedraggable) - [vuedraggable](https://www.npmjs.com/package/vuedraggable)
- [Vuetify](https://vuetifyjs.com/zh-Hans/) 是一个纯手工精心打造的 Material 样式的 Vue UI 组件库。 - [Vuetify](https://vuetifyjs.com/zh-Hans/) 是一个纯手工精心打造的 Material 样式的 Vue UI 组件库。
- [arco-design](https://arco.design/)字节跳动出品的企业级UI库(Vue.js3)
- [ant-design-vue](https://2x.antdv.com/docs/vue/getting-started-cn) 企业级应用的最佳 UI 实践
- [heyui](https://www.heyui.top/): 一个基于Vue.js的高质量UI组件库
[React.js](https://reactjs.org/): A JavaScript library for building user interfaces [React.js](https://reactjs.org/): A JavaScript library for building user interfaces
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册