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

复制代码
相关推荐
Elastic 中国社区官方博客5 小时前
在 Elasticsearch 中使用 Mistral Chat completions 进行上下文工程
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
编程爱好者熊浪6 小时前
两次连接池泄露的BUG
java·数据库
TDengine (老段)8 小时前
TDengine 字符串函数 CHAR 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
qq7422349848 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
姚远Oracle ACE9 小时前
Oracle 如何计算 AWR 报告中的 Sessions 数量
数据库·oracle
Dxy12393102169 小时前
MySQL的SUBSTRING函数详解与应用
数据库·mysql
码力引擎9 小时前
【零基础学MySQL】第十二章:DCL详解
数据库·mysql·1024程序员节
杨云龙UP9 小时前
【MySQL迁移】MySQL数据库迁移实战(利用mysqldump从Windows 5.7迁至Linux 8.0)
linux·运维·数据库·mysql·mssql
l1t9 小时前
利用DeepSeek辅助修改luadbi-duckdb读取DuckDB decimal数据类型
c语言·数据库·单元测试·lua·duckdb
安当加密9 小时前
Nacos配置安全治理:把数据库密码从YAML里请出去
数据库·安全