当前位置:网站首页>Laravel8中的find_in_set、upsert的使用方法
Laravel8中的find_in_set、upsert的使用方法
2022-07-02 06:23:00 【夜空の雪風】
前言
在laravel8中有时候需要使用FIND_IN_SET原生MySql语句,精准查询特殊字符串是否存在指定字符串中解决like无法精准匹配问题。
例如:type字段,type = 1,11,111。type = 2,22,222。采用like模糊查询的时候无法做到精确匹配,就需要用到FIND_IN_SET精确匹配查询。
用法
//DB::table('表名')->whereRaw('FIND_IN_SET(?,字段名)',[需要查询的字符串])->get();
$type = 1;
//代码演示
$data1 = DB::table('file')->whereRaw('FIND_IN_SET(?,type)',[$type])->get();
$data2 = FileModel::whereRaw('FIND_IN_SET(?,type)',[$type])->get();
前言
在解决向数据库表中一次性插入大量数据的时候可以采用upsert方法。在这里放上Laravel8官方文档中的说明。
如果你想在单次查询中执行多个 upsert,那么应该使用 upsert 方法。该方法的第一个参数是由要插入或更新的值组成,而第二个参数列出相应表中惟一标识记录的列,该方法的第三个也是最后一个参数是一个列数组,即如果数据库中已经存在匹配的记录,应该被更新的列。如果模型上启用了时间戳,upsert 方法将自动设置 created_at 和 updated_at 时间戳。
App\Models\Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination'], ['price']);
用法
实际使用:
$insert_data = [
['name' => '文件1', 'use' => '质量', 'type' => 1],
['name' => '文件2', 'use' => '安全', 'type' => 2],
['name' => '文件3', 'use' => '进度', 'type' => 3],
];
FileModel::upsert($insert_data, ['name', 'use', 'type']);//这里写入数据表的字段值
边栏推荐
- Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
- SQL injection closure judgment
- Common function writing method and set get writing method for calculating attributes
- sqli-labs通關匯總-page2
- UEditor .Net版本任意文件上传漏洞复现
- Wechat applet Foundation
- (the 100th blog) written at the end of the second year of doctor's degree -20200818
- CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
- Overload global and member new/delete
- SQLI-LABS通關(less6-less14)
猜你喜欢

sqli-labs通关汇总-page1

In depth study of JVM bottom layer (II): hotspot virtual machine object

There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)

How to debug wechat built-in browser applications (enterprise number, official account, subscription number)

UEditor . Net version arbitrary file upload vulnerability recurrence

The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem

CTF three count

查询GPU时无进程运行,但是显存却被占用了

Linux MySQL 5.6.51 Community Generic 安装教程

Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
随机推荐
SQLI-LABS通关(less2-less5)
Latex在VSCODE中编译中文,使用中文路径问题解决
VSCODE 安装LATEX环境,参数配置,常见问题解决
table 组件指定列合并行方法
SQLI-LABS通关(less1)
Basic knowledge of software testing
Sublime text configuring PHP compilation environment
[daily question] - Huawei machine test 01
Improve user experience defensive programming
Uniapp introduces local fonts
php中根据数字月份返回月份的英文缩写
js中map和forEach的用法
Eslint configuration code auto format
SQL注入闭合判断
2021-07-05C#/CAD二次开发创建圆弧(4)
CTF three count
How to debug wechat built-in browser applications (enterprise number, official account, subscription number)
UEditor .Net版本任意文件上传漏洞复现
Solution to the black screen of win computer screenshot
DeprecationWarning: . ix is deprecated. Please use. loc for label based indexing or. iloc for positi