IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> laravel-orm -> 正文阅读

[PHP知识库]laravel-orm

protected $dateFormat = 'U'; //为时间戳  

const CREATED_AT = 'time1';   //更新列为指定列
const UPDATED_AT = 'updated_at';

protected $connection = 'mysql1';  //连接另外数据库  对应datasebase里面的配置

protected $attributes = [    //给列一个默认值
     'time1' => '2021-10-19',
 ];
 #
 User::query()->take(5)->get();  //take == limit
 //fresh 等于就是再查一次数据库,但是对于当前user实例来说,还是保持以前的 [和refresh]目前没用过待补充
$user = User::find(2);
$userfresh = $user->fresh();

//reject 用法
$user = User::all();
[
//{
//"id": 2,
//"name": "123",
//"email": "294932451@qq.com",
//"email_verified_at": null,
//"created_at": "2021-12-30T08:48:49.000000Z",
//"updated_at": "2021-12-30T08:57:21.000000Z",
//"time1": "2021-10-19 00:00:00"
//},
//{
//    "id": 3,
//		"name": "啊实打实的",
//		"email": "zggMeKTnNvoVvFQl@qq.com",
//		"email_verified_at": null,
//		"created_at": "2021-12-30T08:51:48.000000Z",
//		"updated_at": "2021-12-30T08:51:48.000000Z",
//		"time1": "2021-10-19 00:00:00"
//	},
//{
//    "id": 4,
//		"name": "IvRaqWZXvLsCI93C",
//		"email": "dPhSw4oVNYlKtfSY@qq.com",
//		"email_verified_at": null,
//		"created_at": "2021-12-30T08:52:01.000000Z",
//		"updated_at": "2021-12-30T08:52:01.000000Z",
//		"time1": "2021-10-19 00:00:00"
//	},
//{
//    "id": 5,
//		"name": "xkp1ncyO8qruAacc",
//		"email": "GBVt5I2LaaRU5Hwh@qq.com",
//		"email_verified_at": null,
//		"created_at": "2021-12-30T08:52:01.000000Z",
//		"updated_at": "2021-12-30T08:52:01.000000Z",
//		"time1": "2021-10-19 00:00:00"
//	},
//{
//    "id": 6,
//		"name": "F7P6BOV00SXLexxf",
//		"email": "HwvnOdqbX0TAOxfw@qq.com",
//		"email_verified_at": null,
//		"created_at": "2021-12-30T08:52:02.000000Z",
//		"updated_at": "2021-12-30T08:52:02.000000Z",
//		"time1": "2021-10-19 00:00:00"
//	},
//{
//    "id": 7,
//		"name": "lNrdLtDB15CAoM8E",
//		"email": "xl6iDJcGDXANe5FL@qq.com",
//		"email_verified_at": null,
//		"created_at": "2021-12-30T08:52:02.000000Z",
//		"updated_at": "2021-12-30T08:52:02.000000Z",
//		"time1": "2021-10-19 00:00:00"
//	},
//{
//    "id": 8,
//		"name": "lNkEdkOt69qB1bty",
//		"email": "g2i3SF1ORlOCzSc5@qq.com",
//		"email_verified_at": null,
//		"created_at": "2021-12-30T08:52:03.000000Z",
//		"updated_at": "2021-12-30T08:52:03.000000Z",
//		"time1": "2021-10-19 00:00:00"
//	},

// 删除name==''的模型,说白了就是一个条件
$user = $user->reject(function ($user){
    return $user->name=='啊实打实的';
 });

//chunk使用  每次操作5条
 $user = User::query()->chunk(5,function ($users){
         Log::info($users);
 });
 [2021-12-30 09:25:13] local.INFO: [{"id":2,"name":"123","email":"294932451@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:48:49.000000Z","updated_at":"2021-12-30T08:57:21.000000Z","time1":"2021-10-19 00:00:00"},{"id":3,"name":"\u554a\u5b9e\u6253\u5b9e\u7684","email":"zggMeKTnNvoVvFQl@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:51:48.000000Z","updated_at":"2021-12-30T08:51:48.000000Z","time1":"2021-10-19 00:00:00"},{"id":4,"name":"IvRaqWZXvLsCI93C","email":"dPhSw4oVNYlKtfSY@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-30T08:52:01.000000Z","time1":"2021-10-19 00:00:00"},{"id":5,"name":"xkp1ncyO8qruAacc","email":"GBVt5I2LaaRU5Hwh@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-30T08:52:01.000000Z","time1":"2021-10-19 00:00:00"},{"id":6,"name":"F7P6BOV00SXLexxf","email":"HwvnOdqbX0TAOxfw@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:02.000000Z","updated_at":"2021-12-30T08:52:02.000000Z","time1":"2021-10-19 00:00:00"}]  
[2021-12-30 09:25:13] local.INFO: [{"id":7,"name":"lNrdLtDB15CAoM8E","email":"xl6iDJcGDXANe5FL@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:02.000000Z","updated_at":"2021-12-30T08:52:02.000000Z","time1":"2021-10-19 00:00:00"},{"id":8,"name":"lNkEdkOt69qB1bty","email":"g2i3SF1ORlOCzSc5@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:03.000000Z","updated_at":"2021-12-30T08:52:03.000000Z","time1":"2021-10-19 00:00:00"},{"id":9,"name":"gXSjl81Z444bWwEG","email":"CxzP7CUHnbOskhGx@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:03.000000Z","updated_at":"2021-12-30T08:52:03.000000Z","time1":"2021-10-19 00:00:00"},{"id":10,"name":"JNn1XURxiJUtHvCG","email":"GtQGZ4jzWcq6eV7Y@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:04.000000Z","updated_at":"2021-12-30T08:52:04.000000Z","time1":"2021-10-19 00:00:00"},{"id":11,"name":"RJc3BGyDfB9i3Wqv","email":"kmTqRXyVXeKlC5VY@qq.com","email_verified_at":null,"created_at":"2021-12-30T08:52:04.000000Z","updated_at":"2021-12-30T08:52:04.000000Z","time1":"2021-10-19 00:00:00"}]  


//chunkById  检索更新应该使用此方法,查出email_verified_at is null的数据,然后更改密码为指定密码,返回的是修改之后的模型实例
$user = User::query()->whereNull('email_verified_at')->chunkById(2,function ($users){
 $res = $users->each->update(['password'=>'1234567']);
     Log::info($res);
});
[2021-12-31 03:21:12] local.INFO: [{"id":2,"name":"123","email":"294932451@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:48:49.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"},{"id":3,"name":"\u554a\u5b9e\u6253\u5b9e\u7684","email":"zggMeKTnNvoVvFQl@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:51:48.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"}]  
[2021-12-31 03:21:12] local.INFO: [{"id":4,"name":"IvRaqWZXvLsCI93C","email":"dPhSw4oVNYlKtfSY@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"},{"id":5,"name":"xkp1ncyO8qruAacc","email":"GBVt5I2LaaRU5Hwh@qq.com","email_verified_at":null,"password":"1234567","created_at":"2021-12-30T08:52:01.000000Z","updated_at":"2021-12-31T03:21:12.000000Z","time1":"2021-10-19 00:00:00"}]  

//cursor  使用游标 处理大量数据使用  只会做一次查询
$user = User::cursor();
foreach ($user as $v){
     Log::info($v);
 }
// 加入条件筛选
$users = User::cursor()->filter(function ($user) {
     return $user->id > 20;   //只拿id大于20的
 });
 foreach ($users as $user){
     Log::info($user);
 }

selects 子查询基本使用

航班表
在这里插入图片描述
地区表
在这里插入图片描述
查询到达城市最晚的航班

 $res = Destinations::addselect(['last_flight'=>Flights::select('name')->whereColumn('destination_id','destinations.id')->OrderByDesc('arrived_at')->limit(1)])->get();
 #sql  select `la_destinations`.*, (select `name` from `la_flights` where `destination_id` = `la_destinations`.`id` order by `arrived_at` desc limit 1) as `last_flight` from `la_destinations

在这里插入图片描述

子查询排序

在这里插入图片描述

       $res = Destinations::OrderByDesc(Flights::select('arrived_at')->whereColumn('destination_id','destinations.id')->OrderByDesc('arrived_at')->limit(1))->get();
        dd($res->toArray());
        #select * from `la_destinations` order by (select `arrived_at` from `la_flights` where `destination_id` = `la_destinations`.`id` order by `arrived_at` desc limit 1) desc

在这里插入图片描述

firstOr

在找不到数据的时候会执行回调内的方法,并返回结果

        $model = Destinations::where('id', '>', 3)->firstOr(function () {
           return  Destinations::firstwhere('id',1);
//         return    User::find(2);
        });
        dd($model);

未找到异常

 $flight = Flights::findOrFail(50);
        $flight1 = Flights::where('id', '>', 50)->firstOrFail();
        dd($flight,$flight1);

返回404

检索或创建模型 firstOrCreate firstOrNew

 $flight = Flights::firstOrCreate(
            ['name' => 'London to Paris'],
            ['arrived_at' => '2022-01-01 14:50:37']
        );
        $flight1 = Flights::firstOrNew(
            ['name' => 'London to beijing'],
            ['arrived_at' => '2022-05-01 14:50:37']
        );

在这里插入图片描述
只会增加第一个,第二个需要save才会加入

$flight1->save();
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2022-01-01 13:39:24  更:2022-01-01 13:40:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/14 14:45:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码