laravel学习笔记--数据库篇
执行原生 SQL
执行原生SQL语句还是非常少用的,一般都是用构造器
执行原生 SQL 查询
$users = DB::select('select * from users where id = ?', [1]);
使用命名绑定
除了使用 ? 表示参数绑定外,你还可以使用命名绑定的形式来执行一个查询
$users = DB::select('select * from users where id = :id', ['id' => 1]);
执行普通语句
有些数据库语句不会有任何返回值。对于这些语句,可以使用statement 方法来运行
DB::statement('drop table users');
查询构造器
从一个数据表中获取所有行
$users = DB::table('users')->get();
这种方式的返回值是数组对象,取得值可以这样取
$users[0]->name//[0]是数组中的第几条数组,数组里面的数据就是对象了
获取单条数据
$users = DB::table('users')->where('id', '=', 1)->first();
获取单个字段的值
$users = DB::table('users')->where('id', '=', 1)->value('email');
如果明知道id作为查询条件的话,可以直接使用find()来查询
$users = DB::table('users')->find(1);
获取单列数据
$users = DB::table('users')->pluck('email');
还可以在获取单列数据的同时去指定键名
$users = DB::table('users')->pluck('email', 'name');
聚合
查询构造器还提供了各种聚合方法,比如count, max, min, avg,还有 sum。可以在构造查询后调用任何方法
$users = DB::table('users')->count();//构造器的count方法
$users = DB::table('users')->get()->count();//集合的count方法
判断记录是否存在–返回布尔值
$users = DB::table('users')->where('id', '=', 1)->exists();
判断记录不存在–返回布尔值
$users = DB::table('users')->where('id', '=', 1)->doesntExist();
有时候可能不是总是希望从数据库表中获取所有列。使用 select 方法,你可以自定义一个 select 查询语句来查询指定的字段
$users = DB::table('users')->select('id','name','email')->get();
Join语句
查询构造器也可以编写 join 方法。若要执行基本的「内链接」,你可以在查询构造器实例上使用 join 方法。传递给 join
方法的第一个参数是你需要连接的表的名称,而其他参数则使用指定连接的字段约束。你还可以在单个查询中连接多个数据表
$users = DB::table('users')
->join('c ontacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
Or 语句(或者)
可以一起链式调用 where 约束,也可以在查询中添加 or 子句。 orWhere 方法和 where 方法接收的参数一样
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
还可以使用闭包的方式
如果需要在括号内对 or 条件进行分组,将闭包作为 orWhere 方法的第一个参数也是可以的
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')//$query是已查询的数据
->where('votes', '>', 50);
})
->get();
latest / oldest(日期排序)
latest 和 oldest 方法让你以一种便捷的方式通过日期进行排序。它们默认使用 created_at
列作为排序依据。当然,你也可以传递自定义的列名
$user = DB::table('users')
->latest()
->first();
inRandomOrder(随机排序)
inRandomOrder 方法被用来将结果进行随机排序。例如,你可以使用此方法随机找到一个用户
$randomUser = DB::table('users')
->inRandomOrder()
->first();
groupBy / having(分组)
groupBy 和 having 方法用于将结果分组。 having 方法的使用与 where 方法十分相似
$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();
skip / take(限制数量)
要限制结果的返回数量,或跳过指定数量的结果,你可以使用 skip 和 take 方法
$users = DB::table('users')->skip(10)->take(5)->get();
还可以使用 limit 和 offset 方法去限制数量
$users = DB::table('users')
->offset(10)
->limit(5)
->get();
插入数据库
插入单条数据
$users = DB::table('users')->insert([
'email' => 'xiaoxin@qq.com',
'password' => bcrypt('123456'),
'name' => '小信'
]);
插入多条数据
$users = DB::table('users')->insert([
[
'email' => 'xiaoxin6@qq.com',
'password' => bcrypt('123456'),
'name' => '小信6'
],
[
'email' => 'xiaoxin1@qq.com',
'password' => bcrypt('123456'),
'name' => '大信'
],
[
'email' => 'xiaoxin2@qq.com',
'password' => bcrypt('123456'),
'name' => '小信2'
],
[
'email' => 'xiaoxin3@qq.com',
'password' => bcrypt('123456'),
'name' => '小信3'
],
[
'email' => 'xiaoxin4@qq.com',
'password' => bcrypt('123456'),
'name' => '小信4'
]
]);
获取自增id
如果数据表有自增 ID ,使用 insertGetId 方法来插入记录可以返回 ID 值
$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);
更新
更新时如果不存在匹配记录则创建它
updateOrInsert 方法将首先尝试使用第一个参数的键和值对来查找匹配的数据库记录。
如果记录存在,则使用第二个参数中的值去更新记录。 如果找不到记录,将插入一个新记录,新增的数据是两个数组的集合
DB::table('users')
->updateOrInsert(
['email' => 'john@example.com', 'name' => 'John'],
['votes' => '2']
);
自增自减
increment自增 和 decrement自减
DB::table('users')->increment('count');
注意:当你使用 increment 和 decrement 方法的时候,不会触发模型的事件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构