db
基本使用
db::connection('connection_name');
// 运行数据库查询语句
$results = db::select('select * from users where id = ?', [1]);
$results = db::select('select * from users where id = :id', ['id' => 1]);
// 运行普通语句
db::statement('drop table users');
// 监听查询事件
db::listen(function($sql, $bindings, $time) { code_here; });
// 数据库事务处理
db::transaction(function() {
db::table('users')->update(['votes' => 1]);
db::table('posts')->delete();
});
db::begintransaction();
db::rollback();
db::commit();
// 获取表前缀
db::gettableprefix()
查询语句构造器
// 取得数据表的所有行
db::table('name')->get();
// 取数据表的部分数据
db::table('users')->chunk(100, function($users) {
foreach ($users as $user) {
//
}
});
// 取回数据表的第一条数据
$user = db::table('users')->where('name', 'john')->first();
db::table('name')->first();
// 从单行中取出单列数据
$name = db::table('users')->where('name', 'john')->pluck('name');
db::table('name')->pluck('column');
// 取多行数据的「列数据」数组
$roles = db::table('roles')->lists('title');
$roles = db::table('roles')->lists('title', 'name');
// 指定一个选择字段
$users = db::table('users')->select('name', 'email')->get();
$users = db::table('users')->distinct()->get();
$users = db::table('users')->select('name as user_name')->get();
// 添加一个选择字段到一个已存在的查询语句中
$query = db::table('users')->select('name');
$users = $query->addselect('age')->get();
// 使用 where 运算符
$users = db::table('users')->where('votes', '>', 100)->get();
$users = db::table('users')
->where('votes', '>', 100)
->orwhere('name', 'john')
->get();
$users = db::table('users')
->wherebetween('votes', [1, 100])->get();
$users = db::table('users')
->wherenotbetween('votes', [1, 100])->get();
$users = db::table('users')
->wherein('id', [1, 2, 3])->get();
$users = db::table('users')
->wherenotin('id', [1, 2, 3])->get();
$users = db::table('users')
->wherenull('updated_at')->get();
db::table('name')->wherenotnull('column')->get();
// 动态的 where 字段
$admin = db::table('users')->whereid(1)->first();
$john = db::table('users')
->whereidandemail(2, '[email protected]')
->first();
$jane = db::table('users')
->wherenameorage('jane', 22)
->first();
// order by, group by, 和 having
$users = db::table('users')
->orderby('name', 'desc')
->groupby('count')
->having('count', '>', 100)
->get();
db::table('name')->orderby('column')->get();
db::table('name')->orderby('column','desc')->get();
db::table('name')->having('count', '>', 100)->get();
// 偏移 & 限制
$users = db::table('users')->skip(10)->take(5)->get();
joins
// 基本的 join 声明语句
db::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
// left join 声明语句
db::table('users')
->leftjoin('posts', 'users.id', '=', 'posts.user_id')
->get();
// select * from users where name = 'john' or (votes > 100 and title <> 'admin')
db::table('users')
->where('name', '=', 'john')
->orwhere(function($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'admin');
})
->get();
聚合
$users = db::table('users')->count();
$price = db::table('orders')->max('price');
$price = db::table('orders')->min('price');
$price = db::table('orders')->avg('price');
$total = db::table('users')->sum('votes');
原始表达句
$users = db::table('users')
->select(db::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupby('status')
->get();
// 返回行
db::select('select * from users where id = ?', array('value'));
db::insert('insert into foo set bar=2');
db::update('update foo set bar=2');
db::delete('delete from bar');
// 返回 void
db::statement('update foo set bar=2');
// 在声明语句中加入原始的表达式
db::table('name')->select(db::raw('count(*) as count, column2'))->get();
inserts / updates / deletes / unions / pessimistic locking
// 插入
db::table('users')->insert(
['email' => '[email protected]', 'votes' => 0]
);
$id = db::table('users')->insertgetid(
['email' => '[email protected]', 'votes' => 0]
);
db::table('users')->insert([
['email' => '[email protected]', 'votes' => 0],
['email' => '[email protected]', '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']);
// 删除
db::table('users')->where('votes', '<', 100)->delete();
db::table('users')->delete();
db::table('users')->truncate();
// 集合
// unionall() 方法也是可供使用的,调用方式与 union 相似
$first = db::table('users')->wherenull('first_name');
$users = db::table('users')->wherenull('last_name')->union($first)->get();
// 消极锁
db::table('users')->where('votes', '>', 100)->sharedlock()->get();
db::table('users')->where('votes', '>', 100)->lockforupdate()->get();