Laravel常用数据库操作指令(模型/DB)

1、使用模型操作数据库,需要先引入相应的模型

如:use App\Models\Test;

2、使用DB操作数据库需要先引入DB库

如:use Illuminate\Support\Facades\DB;

一、数据库查询操作

get方法:

复制代码
写法一:Test::select('id','name')->where('id','=',1)->get();//select可以单独查询某几列字段

写法二:Test::where('id','=',1)->get();//去除select后默认去除该表的所有字段

如果上面使用DB类操作,只需要改成:DB::table('name')->where('id','=',1)->get();

DB类操作,遍历值只能是:

复制代码
$v->name;exit;

而模型类操作遍历都可以:

复制代码
$v->name;或者$v['name'];

first方法 ->first():

获取的是单个的集合非数组类的

value方法 :value('name')

方法从纪录中提取单个值

常用的聚合函数:

count()、max()、avg()、min()

existsdoesntExist 查询结果是否存在

distinct ()去重

连表查询:

$users = DB::table('users')

->join('contacts', 'users.id', '=', 'contacts.user_id')

->join('orders', 'users.id', '=', 'orders.user_id')

->select('users.*', 'contacts.phone', 'orders.price')

->get();

where 语句:

第一个值为名称、第二个为操作符号,第三个为值

如果只有两个字段。where会默认使用=号。

常用的where操作运算符:=、>、<、<>、like、

$users = DB::table('users')

->where('votes', '=', 100)

->where('age', '>', 35)

->get();

orWhere():代表或or的关系

闭包

$users = DB::table('users')

->where('votes', '>', 100)

->orWhere(function(Builder query) { query->where('name', 'Abigail')

->where('votes', '>', 50);

})

->get();

select * from users where votes > 100 or (name = 'Abigail' and votes > 50)

Where Not

下面的查询排除了正在清仓甩卖或价格低于

$products = DB::table('products')

->whereNot(function (Builder query) { query->where('clearance', true)
->orWhere('price', '<', 10);
})
->get();

whereBetween / orWhereBetween

whereBetween 方法是用来验证字段的值是否在给定的两个值之间

$users = DB::table('users')

->whereBetween('votes', [1, 100])

->get();

whereNotBetween / orWhereNotBetween

whereIn 方法用于验证字段是否在给定的值数组中

$users = DB::table('users')

->whereIn('id', [1, 2, 3])

->get();

$users = DB::table('users')

->whereNotIn('id', [1, 2, 3])

->get();

whereNull 方法用于判断指定的字段的值是否是 NULL

$users = DB::table('users')
->whereNull('updated_at')
->get();

whereNotNull 方法是用来验证给定字段的值是否不为 NULL:

whereDate / whereMonth / whereDay / whereYear / whereTime

$users = DB::table('users')

->whereDate('created_at', '2016-12-31')

->get();

$users = DB::table('users')

->whereMonth('created_at', '12')

->get();

whereColumn 方法是用来比较两个给定字段的值是否相等:

inRandomOrder 方法被用来将查询结果随机排序。例如,你可以使用这个方法去获得一个随机用户

latestoldest 方法可以方便让你把结果根据日期排序。查询结果默认根据数据表的 created_at

如你所愿,groupByhaving 方法可以将查询结果分组。having 方法的使用方法类似于 where 方法

​​​​​​​

或者,你可以使用 limitoffset 方法。这些方法在功能上等同于 takeskip 方法,如下

复制代码
相关推荐
在努力的前端小白13 分钟前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务15 分钟前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
冒泡的肥皂4 小时前
MVCC初学demo(一
数据库·后端·mysql
.Shu.5 小时前
Redis Reactor 模型详解【基本架构、事件循环机制、结合源码详细追踪读写请求从客户端连接到命令执行的完整流程】
数据库·redis·架构
fakaifa5 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
薛晓刚7 小时前
当MySQL的int不够用了
数据库
SelectDB技术团队8 小时前
Apache Doris 在菜鸟的大规模湖仓业务场景落地实践
数据库·数据仓库·数据分析·apache doris·菜鸟技术
星空下的曙光8 小时前
mysql 命令语法操作篇 数据库约束有哪些 怎么使用
数据库·mysql
小楓12018 小时前
MySQL數據庫開發教學(一) 基本架構
数据库·后端·mysql
染落林间色8 小时前
达梦数据库-实时主备集群部署详解(附图文)手工搭建一主一备数据守护集群DW
数据库·sql