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 方法,如下

复制代码
相关推荐
心平愈三千疾18 分钟前
通俗理解JVM细节-面试篇
java·jvm·数据库·面试
我科绝伦(Huanhuan Zhou)9 天前
Oracle|Oracle SQL*Plus 配置上下翻页功能
数据库·sql·oracle
Cachel wood9 天前
Spark教程6:Spark 底层执行原理详解
大数据·数据库·分布式·计算机网络·spark
java—大象9 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
Mutig_s9 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis
Python小老六9 天前
单片机测ntc热敏电阻的几种方法(软件)
数据库·单片机·嵌入式硬件
矿渣渣9 天前
SQLite3 在嵌入式系统中的应用指南
数据库·sqlite·嵌入式实时数据库
@昵称不存在9 天前
Python csv 模块
开发语言·数据库·python
程序猿小D9 天前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+Vue实现的校园二手交易平台管理系统,推荐!
java·数据库·mysql·spring·vue·毕业设计·校园二手交易平台
DoWeixin69 天前
【请关注】hBase要用的顺畅的思路
数据库