laravel-14-笔记-2
5.数据库基本操作
<!-- 配置 -->
config/app.php
config/datebase.php
.env
<!-- 基本操作 -->
<!-- 查询 -->
DB::select
use DB;
$res = DB::select("select * from goods where id < 50");
<!--预处理 -->
$res = DB::select("select * from goods where id = ?", [20]);
echo "<pre>";
var_dump($res);
<!-- 插入 -->
DB::insert
use DB;
$res = DB::insert("insert into goods (title, cid, price, pic) values('衣服', '23', '455', '3.jpg')");
<!-- 预处理 -->
$res = DB::insert("insert into goods (title, cid, price, pic) values(?, ?, ?, ?)", ['上衣', 20, 599, '1.jpg']);
<!-- 修改 -->
DB::update
use DB;
$res = DB::update("update goods set title ='上衣' where id = 2");
<!-- 预处理 -->
$res = DB::update("update goods set title = ? where id = ? ", ['裤子', 1]);
<!-- 删除 -->
DB::delete
use DB;
$res = DB::delete("delete from goods where id < 10");
<!-- 预处理 -->
$res = DB::delete("delete from goods where id > ? ", [20]);
<!-- 一般语句 -->
DB::statement();
use DB;
DB::statement('drop table users');
DB::statement('create table test(id int primary key auto_increment, name varchar(10) )');
<!-- 事务 -->
<!-- 开启事务 -->
DB::beginTransaction();
<!-- 扣钱 -->
$res = DB::update('update user set account = account - 200 where id = 1');
$res2 = DB::update('update user set account = account + 200 where id = 2');
if($res && $res2) {
<!-- 事务提交 -->
DB::commit();
echo '转账成功!';
} else {
<!-- 事务回滚 -->
DB::rollback();
echo '转账失败!';
}
<!-- 操作多个数据库 -->
<!-- 添加多个数据库链接 -->
config/datebase.php
<!-- 操作多个数据库 -->
$res = DB::connection('mysql1')->select();//insert update delete
<!-- 构造器 -->
<!-- 增删改查 -->
<!-- 插入 -->
insert();
<!-- 单条插入 -->
$res = DB::table('user')->insert([
'username' => 'ac',
'password' => 'ac',
'account' => '100'
]);
<!-- 多条插入 -->
$res = DB::table('user')->insert([
['username'=>'ab1', 'password'=>'ccc', 'account'=>200],
['username'=>'ab2', 'password'=>'www', 'account'=>300],
['username'=>'ab3', 'password'=>'eee', 'account'=>500],
]);
<!-- 获取id插入 -->
$res = DB::table('user')->insertGetId([
'username'=>'ee1',
'password'=>'qq2',
'account'=>232
]);
<!-- 更新 -->
update()
$res = DB::table('user')->where('id', '=', 2)->update(['username'=>'bbp']);
<!-- 删除 -->
delete()
$res = DB::table('user')->where('id', "<", 10)->delete();
<!-- 查询 -->
select()
<!-- 查询多条 -->
$res = DB::table('user')->get();
<!-- 获取单条 -->
$res = DB::table('user')->first();
<!-- 获取某个字段的值 -->
$res = DB::table('user')->value('title');
<!-- 获取某一列的数据 -->
$res = DB::table('user')->lists('username');
<!-- 连贯操作 -->
<!-- 设置字段 -->
DB::table('user')->select('username', 'password', 'age')->get();
<!-- 条件 -->
DB::table('user')->where('username', '=', 'aaa')->first();
<!-- orWhere -->
DB::table('user')->where('id', '=', '1')->orWhere('username', '=', 'aaa')->get();
<!-- whereBetween -->
DB::table('user')->whereBetween('id', [1, 100])->get();
<!-- whereIn -->
DB::table('user')->whereIn('id', [1,4,6])->get();
<!-- 排序 -->
DB::table('user')->orderBy('name', 'desc');
<!-- 分页 跳过5条 提取4条-->
DB::table('user')->skip(5)->take(4)->get();
<!-- 连接表 -->
DB::table('goods')->leftJoin('cate', 'cate.id', '=', 'goods.cid')->where('goods.cid', '<', 10)->get();
<!-- 计算 -->
<!-- 总数 -->
DB::table('user')->where('id', '>', 100)->count();
<!-- 最大值 -->
DB::table('user')->max('price');
<!-- 最小值 -->
DB::table('user')->min('price');
<!-- 平均值 -->
DB::table('user')->avg('price');
<!-- sql语句记录 -->
<!-- 在路由中添加 -->
Event::listen('illuminate.query',function($query){var_dump($query)});