Laravel查询构造器的使用方法整理

1.结果集

1.1从一张表获取所有行,get方法获取所有行

$users = DB::table('users')->get();

获取列的值

foreach ($users as $user) { 
echo $user->name;
}

1.2.从一张表中获取一行/一列,first方法获取单行

$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;

1.3.如果想要获取包含单个列值的数组,可以使用lists方法

$titles = DB::table('roles')->lists('title');
foreach ($titles as $title) {
echo $title;

1.4.聚合函数,比如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')

1.5.我们并不总是想要获取数据表的所有列,使用select方法查询指定列

$users = DB::table('users')->select('name', 'email as user_email')->get();

1.6.distinct方法允许你强制查询返回不重复的结果集

$users = DB::table('users')->distinct()->get();

1.7.内连接

$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

1.8.左连接

$users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();

2.where子句

调用where最基本的方法需要三个参数,第一个参数是列名,第二个参数是一个数据库系统支持的任意操作符,第三个参数是该列要比较的值。

例如,下面是一个验证“votes”列的值是否等于100的查询:

$users = DB::table('users')->where('votes', '=', 100)->get();

为了方便,如果你只是简单比较列值和给定数值是否相等,可以将数值直接作为where方法的第二个参数:

$users = DB::table('users')->where('votes', 100)->get();

其它操作符来编写where子句:

$users = DB::table('users') 
->where('votes', '>=', 100)
->get();
$users = DB::table('users')
->where('votes', '<>', 100)
->get();
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();

你可以通过方法链将多个where约束链接到一起,也可以添加or子句到查询,orWhere方法和where方法接收参数一样:

$users = DB::table('users')
 ->where('votes', '>', 100)
 ->orWhere('name', 'John')
 ->get();

whereBetween方法验证列值是否在给定值之间(whereNotBetween方法验证列值不在给定值之间):

$users = DB::table('users')
->whereBetween('votes', [1, 100])
->get();

whereIn方法验证给定列的值是否在给定数组中(whereNotIn方法验证给定列的值不在给定数组中):

$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();

whereNull方法验证给定列的值为NULL(whereNotNull方法验证给定列的值不是NULL):

$users = DB::table('users') 
->whereNull('updated_at')
->get();

3.orderBy方法

$users = DB::table('users') 
->orderBy('name', 'desc')
->get();

4.groupBy和having方法用于对结果集进行分组

$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();

ps:a.WHERE 子句用来筛选 FROM 子句中指定的操作所产生的行。
     b.GROUP BY 子句用来分组 WHERE 子句的输出。
     c.HAVING 子句用来从分组的结果中筛选行。

5.skipskip/take,想要限定查询返回的结果集的数目,或者在查询中跳过给定数目的结果

$users = DB::table('users')->skip(10)->take(5)->get();

6.insert方法

DB::table('users')
->insert([ ['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0]
]);

如果数据表有自增ID,使用insertGetId方法来插入记录将会返回ID值:

$id = DB::table('users')
->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);

7.update方法

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']);

8.delete方法

DB::table('users')->delete();

在调用delete方法之前可以通过添加where子句对delete语句进行约束:

DB::table('users')->where('votes', '<', 100)->delete();

如果你希望清除整张表,也就是删除所有列并将自增ID置为0,可以使用truncate方法:

DB::table('users')->truncate();
原文地址:https://www.cnblogs.com/lamp01/p/6666514.html