查询
获取多条数据
$users = DB::table('users')->get();
获取单条数据
$user = DB::table('users')->where('name', 'John')->first();
获取单条数据单个列
$name = DB::table('users')->where('name', 'John')->pluck('name');
获取单个值的多条数据
$roles = DB::table('roles')->lists('title');
获取数组的kv列表
DB::table('roles')->pluck('title','key');
按字段条件去重
DB::table('roles')->distinct('key');
只返回某个字段的值,以string返回
DB::table('roles')->value('title');
查询中应用查询
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();
查询条件
$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', array(1, 100))->get();
$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNull('updated_at')->get();
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
$users = DB::table('users')->skip(10)->take(5)->get(); //Offset & Limit
复合查询
- 基础的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();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
- 分组
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')
DB::table('users')
->whereExists(function(query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
聚合查询
$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::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, array('name' => 'John'));
插入
- 插入
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
);
- 插入后获取自增ID
$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);
- 多条记录一起插入
DB::table('users')->insert(array(
array('email' => 'taylor@example.com', 'votes' => 0),
array('email' => 'dayle@example.com', 'votes' => 0),
));
更新
DB::table('users')->where('id', 1)->update(array('votes' => 1));
删除
- 删除指定条件的数据
DB::table('users')->where('votes', '<', 100)->delete();
- 删除表里所有数据
DB::table('users')->delete();
- 删除这个表
DB::table('users')->truncate();
联盟2个查询; unionAll方法也可以,有相同的方法签名。
$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();
缓存
查询的结果将为十分钟被缓存。查询结果缓存时,不会对数据库运行,结果将从默认的缓存加载驱动程序指定您的应用程序。
$users = DB::table('users')->remember(10)->get();
如果您使用的是支持缓存的司机,还可以添加标签来缓存:
$users = DB::table('users')->cacheTags(array('people', 'authors'))->remember(10)->get();