在上一篇使用迁移定义好资料库的架构後,我们还需要学习如何与资料库互动,在 Laravel 中我们可以不必使用原生的 SQL 语法来操作资料库,Laravel 提供一种流畅的语法来取代这些操作,比起原生语法更直觉方便和安全。
今天假设我有一资料表 users 存放使用者的资料,在使用原生 SQL 要取得所有资料时,会使用这样的语句:
select * from `users`
而若要改使用查询建构器取得所有资料时,Laravel 支援 DB 静态呼叫的方法:
/* 使用查询建构器时,需使用 DB 的名称空间 */
use Illuminate\Support\Facades\DB;
DB::table('users')->get();
上面使用 SQL 和查询建构器的两种语法结果是相同的,下面会介绍许多常用的查询建构器。
/* 只会回传 name 和 password 两个栏位的资料 */
DB::table('users')->select('name', 'password')->get();
/* 取得 id = 1 的资料 */
DB::table('users')->where('id', '1')->get();
/* 取得 id > 1 的资料 */
DB::table('users')->where('id', '>', '1')->get();
/* 取得 id = 1 或 name = Tom 的资料 */
DB::table('users')->where('id', '1')->orWhere('name', 'Tom')->get();
这边要注意到如果使用多个 where() 与 orWhere() 可能会造成查询语句上的混淆,各位可以思考看看下面这样的查询建构器实际上会取的怎样的资料:
DB::table('users')->where('id', '>', '1')->orWhere('name', 'Tom')->where('password', '3456')->get();
上面的查询建构器实际上会符合这样的 SQL 语句:
select * from `users` where `id` > 1 or `name` = 'Tom' and `password` = '3456'
这边会发现 where() 是使用 and 去衔接,而 orWhere() 则是使用 or,由於 and 的优先度会高於 or,在使用上务必注意此点。
/* 取得 id 2 ~ 5 的资料 */
DB::table('users')->whereBetween('id', [2, 5])->get();
/* 取得 id 不在 2 ~ 5 的资料 */
DB::table('users')->whereNotBetween('id', [2, 5])->get();
/* 取得 id = 2 和 5 的资料 */
DB::table('users')->whereIn('id', [2, 5])->get();
/* 此范例为一错误示范,id 为资料表的主键栏位,代表 id 不会有重覆值,使用 distinct() 做查询的话会没有结果 */
DB::table('users')->select('id')->distinct()->get();
/* 第二个参数支援 asc(升序,预设) 与 desc(降序) */
DB::table('users')->orderBy('id', 'desc')->get();
/* 由於 city 栏位中有两组 Tatpei 资料,所以只会得到四笔资料(Jhon 和 Zorn 分为一组,所以只取得 John 的资料,再加上 James、Tom、Jack 三笔总共四笔) */
DB::table('users')->groupBy('city')->get();
/* 跳过第 1 笔取得後面头 2 笔的资料,最常用来制作分页 */
DB::table('users')->skip(1)->take(2)->get();
/* 回传 John */
DB::table('users')->value('name');
/* 回传 6 */
DB::table('users')->count();
/* 回传 195 */
DB::table('users')->sum('age');
/* 回传 32.5 */
DB::table('users')->avg('age');
/* 符合连结条件的资料才会显示 */
DB::table('users')->join('contacts', 'users.id', '=', 'contacts.user_id')->get();
/* 左侧资料表的所有资料会加入查询结果,即使没有符合连结条件的资料也会显示 */
DB::table('users')->leftJoin('contacts', 'users.id', '=', 'contacts.user_id')->get();
DB::table('users')->insert([
['email' => '[email protected]', 'age' => 36],
['email' => '[email protected]', 'age' => 48],
]);
DB::table('users')
->where('id', 1)
->update(['age' => 16]);
DB::table('users')->where('id', 2)->delete();
/* 取得所有用户居住城市的分组资料 */
DB::table('users')->select(DB::raw('city, count(city) as count'))->groupBy('city')->get();
<<: [无广告]自动封锁,诈骗电话,骚扰电话,行销,广告,推销,来电未显示,不明的电话,响一声就挂,一接就挂,一接秒挂
嘿,今天是怎样? 都没有人交作业,是不是昨天的太小菜一叠了! 今天是昨天的延伸, 但说难也难不到哪...
"AE、AES、AED、AEIOU。" --- 密钥排程 key schedul...
C 是一种通用的高阶语言,最开始是在 1972 年的时候,於 DEC PDP-11 计算机上被第一次...
calc() 是一个 CSS 的函数,功能如 function 字面上的意思,在设定属性的时候可以进...
图片来源:Bitsorbit CVE列表是指特定产品或系统中已识别的漏洞。与未发现或未知的漏洞相比...