[李景山php]每天laravel[033]-laravel 基础知识 --- 数据库
2017-04-08 10:30
615 查看
1 基础配置
1.1 laravel 支持的数据库有,MySQL,Postgres,SQLite,SQL Server
1.2 数据库配置文件位置 config/database.php
1.3 支持读写分离的配置
1.4 基础使用 方式 增删改查 及其它通常操作
一般使用
绑定名字
更新
删除
通常的语句
1.5 调试方式,注册query 监听器
在我们的 AppServiceProvider 里面选择boot函数添加,监听函数如下
1.6 事务支持
自动事务处理
手动事务支持
1.7 更多数据库及PDO方式支持
更多数据库连接灵活选择
使用PDO的连接实例化
2 查询构造器
2.1 获取全部信息
2.2 返回单条数据
返回单条数据里面的单个字段
2.3 分解表格结果
如果当内部的闭包匿名函数返回错误的时候,停止分解表格
2.4 取出一列数据作为一个集合
普通取出
增加key的方式取出
2.5 计算聚合结果如:count,max,min,avg,sum 实例代码如下:
2.6 选择数据的使用
基础使用
去重选择
二次编辑选择字段
使用原生表达式的选择方式
2.7 joins 联合查询
默认的join使用
左链接
更复杂的 join 查询
或者可以使用 orOn 或者是 orWhere 方式
2.8 联合数据结果 Unions
2.9 强大的 where 查询操作
基础的
基础 等于 简化版本
其它关系操作
or条件的拼合
whereBetween 方式
whererNotBetween
whereIn/whereNoIn/whereNull/whereNotNull
高阶where条件查询
翻译过来:
exists Statemets 语句条件
结果为:
Ordering,Grouping,Limit,Offset 设置
对结果集进行处理
2.10 插入数据
普通插入
多行插入
返回自增长ID
更新数据:
增长/减少
可以混合增长
2.11 删除信息/ 清空数据库
2.12 锁
1.1 laravel 支持的数据库有,MySQL,Postgres,SQLite,SQL Server
1.2 数据库配置文件位置 config/database.php
1.3 支持读写分离的配置
'mysql' => [ 'read' => [ 'host' => '192.168.1.1', ], 'write' => [ 'host' => '196.168.1.2' ], 'driver' => 'mysql', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '',],
1.4 基础使用 方式 增删改查 及其它通常操作
一般使用
$user = DB::select('select * from users where active = ?',[1]);
绑定名字
$result = DB::insert('insert into users (id,name) values (?,?)',[1,'JingShan']);
更新
$affected = DB::update('update users set votes = 1000 where name = ?',['john']);
删除
$deleted = DB::delete('delete from users');
通常的语句
DB::statement('drop table users');
1.5 调试方式,注册query 监听器
在我们的 AppServiceProvider 里面选择boot函数添加,监听函数如下
public function boot(){ DB::listen(function($sql,$binding,$time){ // 执行监听的逻辑 }); }
1.6 事务支持
自动事务处理
DB::transaction(function(){ DB::table('users')->update(['votes'=>1]); DB::table('posts')->delete(); });
手动事务支持
DB::beginTransaction(); DB::rollBack(); DB::commit();
1.7 更多数据库及PDO方式支持
更多数据库连接灵活选择
$users = DB::connection('foo')->select();
使用PDO的连接实例化
$pdo = DB::connection()->getPdo();
2 查询构造器
2.1 获取全部信息
$users = DB::table('users')->get();
2.2 返回单条数据
$user = DB::table('users')->where('name','jhon')->first();
返回单条数据里面的单个字段
$email = DB::table('users')->where('name','Jhon')->value('email');
2.3 分解表格结果
DB::table('users')->chunk(100,function($users){ foreach($users as $user){ // TODO } });
如果当内部的闭包匿名函数返回错误的时候,停止分解表格
DB::table('users')->chunk(100,function($users){ // TODO return false;// 返回false 就停止后面的拆分了。 });
2.4 取出一列数据作为一个集合
普通取出
$titles = DB::table('roles')->lists('title'); foreach($titles as $title){ echo $title; }
增加key的方式取出
$roles = DB::table('roles')->lists('title','name'); foreach($roles as $name => $title){ echo $title; }
2.5 计算聚合结果如:count,max,min,avg,sum 实例代码如下:
$users = DB::table('users')->count(); $price = DB::table('orders')->max('price'); $price = DB::table('orders')->where('finalized',1)->avg('price');
2.6 选择数据的使用
基础使用
$users = DB::tables('users')->select('name','email as user_email')->get();
去重选择
$users = DB::table('users')->distinct()->get();
二次编辑选择字段
$query = DB::table('users')->select('name'); $users = $query->addSelect('age')->get();
使用原生表达式的选择方式
$users = DB::table('users') ->select(DB::raw('count(*) as user_count ,status')) ->where('status','<>',1) ->groupBy('status') ->get()
2.7 joins 联合查询
默认的join使用
$user = DB::table('users') ->join('contacts','users.id','=','contacts.user_id') ->join('orders','users.id','=','orders.user_id') ->select('users.*','contacts.phone','orders.price') ->get();
左链接
$user = DB::table('users') ->leftJoin('posts','users.id','=','posts.user_id') ->get();
更复杂的 join 查询
DB::table('users') ->join('contacts',function($join){ $join->on('users_id','=','contacts.user_id') ->where('contacts.user_id','>',5); }) ->get();
或者可以使用 orOn 或者是 orWhere 方式
2.8 联合数据结果 Unions
$first = DB::table('users') ->whereNull('first_name'); $users = DB::table('users') ->whereNull('last_name') ->union($first) ->get();
2.9 强大的 where 查询操作
基础的
$users = DB::table('users')->where('votes','=',100)->get();
基础 等于 简化版本
$users = DB::table('users')->where('votes',100)->get();
其它关系操作
$users = DB::table('users')->where('votes','>=',100)->get(); $users = DB::table('users')->where('votes','<>',100)->get(); $users = DB::table('users')->where('votes','like=','%a%')->get();
or条件的拼合
$users = DB::table('users') ->where('votes','>',100) ->orWhere('name','John') ->get();
whereBetween 方式
$users = DB::table('users')->whereBetween('votes',[1,100])->get();
whererNotBetween
$users = DB::table('users')->whereNotBetween('votes',[1,100])->get();
whereIn/whereNoIn/whereNull/whereNotNull
$users = DB::table('users')->whereIn('id',[1,2,4])->get(); $users = DB::table('users')->whereNotIn('id',[1,2,4])->get(); $users = DB::table('users')->whereNull('id')->get(); $users = DB::table('users')->whereNotNull('id')->get();
高阶where条件查询
DB::table('users') ->where('name','=','John') ->orWhere(function($query){ $query->where('votes','>',100) ->where('title','<>','Admin'); }) ->get();
翻译过来:
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
exists Statemets 语句条件
DB::table('users') ->whereExists(function($query){ $query->select(DB::raw(1)) ->from('orders') whereRaw('orders.user_id = users.id '); }) ->get();
结果为:
select * from users where exits (select 1 from orders where orders.user_id = users.id )
Ordering,Grouping,Limit,Offset 设置
$users = DB::tabel('users') ->orderBy('name','desc') ->get(); $users = DB::table('users') ->groupBy('account_id') ->having('account_id','>',100) ->get(); $users = DB::table('orders') ->select('department',DB::raw('SUM(price) as total_sales')) ->groupBy('department') ->havingRaw('SUM(price) > 2500') ->get();
对结果集进行处理
$users = DB::table('users')->skip(10)->take(5)->get();
2.10 插入数据
普通插入
DB::table('users')->insert(['email'=>'john@example','votes'=>0]);
多行插入
DB::table('users')->insert([ ['email'=>'taylor@example.com','votes'=>0], ['email'=>'123@example.com','votes'=>0 ] ]);
返回自增长ID
$id = DB::table('users')->insertGetId(['email'=>'john@example.com','votes'=>0]);
更新数据:
DB::table('users') ->where('id',1) ->update(['votes'=>1]);
增长/减少
DB::table('users')->increment('votes'); DB::table('users')->increment('votes',5); DB::table('users')->decrement('votes'); DB::table('users')->decrement('votes',5);
可以混合增长
DB::table('users')->increment('votes',1,['name'=>'John']);
2.11 删除信息/ 清空数据库
DB::table('users')->delete(); DB::table('users')->where('votes','<',100)->delete(); DB::table('users')->truncate();// 清空数据库
2.12 锁
DB::table('users')->where('votes','>',100)->shareLock()->get(); DB::table('users')->where('votes','>',100)->lockForUpdate()->get();
相关文章推荐
- [李景山php]每天laravel[034]-laravel 基础知识 --- 数据迁移及填充
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---在主页上添加数据
- [李景山php]每天laravel[025]-laravel 基础知识 --- 控制器
- [李景山php]每天laravel[035]-laravel 基础知识 --- Eloquent 模型之关系映射
- [李景山php]每天laravel[031]-laravel 基础知识 --- 服务容器
- [李景山php]每天laravel[030]-laravel 基础知识 --- blade模版
- [李景山php]每天laravel[028]-laravel 基础知识 --- 响应
- [李景山php]每天laravel[025]-laravel 基础知识 --- 中间件
- [李景山php]每天laravel[027]-laravel 基础知识 --- 请求
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---删除数据
- [李景山php]每天laravel[032]-laravel 基础知识 --- Contract 及 Facade
- [李景山php]每天laravel[029]-laravel 基础知识 --- 视图
- [李景山php]每天laravel[024]-laravel 基础知识 --- 路由
- [李景山php]每天laravel[017]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---准备数据库
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---主页,亲爱的,我回来了!!
- [李景山php]每天laravel[022]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---Task 任务处理控制器--显示主页
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---登录,该死的登录
- [李景山php]每天laravel[020]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---视图
- [李景山php]每天laravel[018]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---准备关系模型
- [李景山php]每天laravel[019]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---路由