laravel 使用构造器进行增删改查
使用原生语句进行增删改查
//$list = DB::select('select * from wt_category where id = :id', ['id' => 34]);
//$insert = DB::insert('insert into wt_category (cate_name, orders) values (?, ?)', ['框架.laravel', 3]);
//echo "<pre>";
//var_dump($insert);
//return $this->Member->getInfos();
//return Member::getMember();
使用构造器:
获取结果
从数据表中获取所有的数据
你可以在 DB
facade 上使用 table
方法开始查询。这个 table
方法为给定的表返回一个查询构造器实例,允许你在查询上链式调用更多的约束,最后使用 get
方法获取最终结果:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* 显示所有应用程序用户的列表
*
* @return Response
*/
public function index()
{
$users = DB::table('users')->get();
return view('user.index', ['users' => $users]);
}
}
get
方法会返回一个包含 Illuminate\Support\Collection
的结果,其中每个结果都是一个 PHP StdClass
对象的一个实例。你可以通过访问字段作为对象的属性来访问每列的值:
foreach ($users as $user) {
echo $user->name;
}
从数据表中获取单个列或行
如果你只需要从数据库表中获取一行数据,就使用 first
方法。这个方法将返回一个 StdClass
对象:
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;
如果你甚至不需要整行数据,就使用 value
方法从记录中取出单个值。该方法将直接返回字段的值:
$email = DB::table('users')->where('name', 'John')->value('email');
获取一列的值
如果你想要获取包含单个字段值的集合,可以使用 pluck
方法。在下面的例子中,我们将取出 roles 表中 title 字段的集合:
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $title) {
echo $title;
}
你也可以在返回的集合中指定字段的自定义键值:
$roles = DB::table('roles')->pluck('title', 'name');
foreach ($roles as $name => $title) {
echo $title;
}
结果分块
如果你需要操作数千条数据库记录,可以考虑使用 chunk
方法。这个方法每次只取出一小块结果传递给 闭包
处理,这对于编写数千条记录的 Artisan 命令 而言是非常有用的。例如,一次处理整个 users
表中的 100 个记录:
DB::table('users')->orderBy('id')->chunk(100, function ($users) {
foreach ($users as $user) {
//
}
});
你可以从 闭包
中返回 false
来阻止进一步的分块的处理:
DB::table('users')->orderBy('id')->chunk(100, function ($users) {
// Process the records...
return false;
});
聚合
查询构造器还提供了各种聚合方法,如 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');
Selects
指定一个 Select 语句
你并不会总是想从数据表中选出所有的字段,这时可使用 select
方法自定义一个 select
语句来指定查询的字段:
$users = DB::table('users')->select('name', 'email as user_email')->get();
distinct
方法允许你强制让查询返回不重复的结果:
$users = DB::table('users')->distinct()->get();
如果你已有一个查询构造器实例,并且希望在现有的 select 语句中加入一个字段,则可以使用 addSelect
方法:
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();
原生表达式
有时候你可能需要在查询中使用原生表达式,使用 DB::raw
方法可以创建原生表达式:
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
{note} 原生表达式将会被当作字符串注入到查询中,所以要小心避免创建 SQL 注入漏洞。
原生方法
可以使用以下的方法代替 DB::raw
将原生表达式插入查询的各个部分。
selectRaw
selectRaw
方法可以用来代替 select(DB::raw(...))
。这个方法的第二个参数接受一个可选的绑定参数的数组:
$orders = DB::table('orders')
->selectRaw('price * ? as price_with_tax', [1.0825])
->get();
whereRaw / orWhereRaw
可以使用 whereRaw
和 orWhereRaw
方法将原生的 where
语句注入到查询中。这些方法接受一个可选的绑定数组作为他们的第二个参数:
$orders = DB::table('orders')
->whereRaw('price > IF(state = "TX", ?, 100)', [200])
->get();
havingRaw / orHavingRaw
havingRaw
和 orHavingRaw
方法可用于将原生字符串设置为 having
语句的值:
$orders = DB::table('orders')
->select('department', DB::raw('SUM(price) as total_sales'))
->groupBy('department')
->havingRaw('SUM(price) > 2500')
->get();
orderByRaw
orderByRaw
方法可用于将原生字符串设置为 order by
语句的值:
$orders = DB::table('orders')
->orderByRaw('updated_at - created_at DESC')
->get();
Joins
Inner Join 语句
查询构造器也可以编写 join 语句。若要执行基本的「内连接」,你可以在查询构造器实例上使用 join
方法。传递给 join
方法的第一个参数是你要需要连接的表的名称,而其它参数则用来指定连接的字段约束。你还可以在单个查询中连接多个数据表:
$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();
Left Join 语句
如果你想用「左连接」来代替「内连接」,请使用 leftJoin
方法。leftJoin
方法的用法和 join
方法一样:
$users = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
Cross Join 语句
使用 crossJoin
方法和你想要交叉连接的表名来做「交叉连接」。交叉连接在第一个表和连接之间生成笛卡尔积:
$users = DB::table('sizes')
->crossJoin('colours')
->get();
高级 Join 语句
你也可以指定更高级的 join 语句。比如传递一个 闭包
作为 join
方法的第二个参数。此 闭包
接收一个 JoinClause
对象,从而在其中指定 join
语句中指定约束:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
如果你想要在连接上使用「where」风格的语句,可以在连接上使用 where
和 orWhere
方法。这些方法可以用来比较值和对应的字段:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
Unions
查询构造器还提供了将两个查询「合并」起来的快捷方式。例如,你可以先创建一个初始查询,并使用 union
方法将它与第二个查询进行合并:
$first = DB::table('users')
->whereNull('first_name');
$users = DB::table('users')
->whereNull('last_name')
->union($first)
->get();
{tip} 也可使用
unionAll
方法,它和union
方法有着相同的用法。
Where 语句
简单的 Where 语句
你可以在查询构造器实例中使用 where
方法从而把 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
函数:
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
Or 语句
你可以一起链式调用 where,也可以在查询添加中 or
语句。orWhere
方法接受与 where
方法相同的参数:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
其它 Where 语句
whereBetween
whereBetween
方法用来验证字段的值介于两个值之间:
$users = DB::table('users')
->whereBetween('votes', [1, 100])->get();
whereNotBetween
whereNotBetween
方法验证字段的值不在两个值之间:
$users = DB::table('users')
->whereNotBetween('votes', [1, 100])
->get();
whereIn / whereNotIn
whereIn
方法验证字段的值在指定的数组内:
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
whereNotIn
方法验证字段的值不在指定的数组内:
$users = DB::table('users')
->whereNotIn('id', [1, 2, 3])
->get();
whereNull / whereNotNull
whereNull
方法验证字段的值为 NULL
:
$users = DB::table('users')
->whereNull('updated_at')
->get();
whereNotNull
方法验证字段的值不为 NULL
:
$users = DB::table('users')
->whereNotNull('updated_at')
->get();
whereDate / whereMonth / whereDay / whereYear / whereTime
whereDate
方法用于比较字段的值和日期:
$users = DB::table('users')
->whereDate('created_at', '2016-12-31')
->get();
whereMonth
方法用于比较字段的值与一年的特定月份:
$users = DB::table('users')
->whereMonth('created_at', '12')
->get();
whereDay
方法用于比较字段的值与特定的一个月的某一天:
$users = DB::table('users')
->whereDay('created_at', '31')
->get();
whereYear
方法用于比较字段的值与特定年份:
$users = DB::table('users')
->whereYear('created_at', '2016')
->get();
whereTime
方法用于比较字段的值与特定的时间:
$users = DB::table('users')
->whereTime('created_at', '=', '11:20')
->get();
whereColumn
whereColumn
方法用于验证两个字段是否相等:
$users = DB::table('users')
->whereColumn('first_name', 'last_name')
->get();
还可以将比较运算符传递给该方法:
$users = DB::table('users')
->whereColumn('updated_at', '>', 'created_at')
->get();
whereColumn
方法也可以传递一个包含多个条件的数组。这些条件将使用 and
运算符进行连接:
$users = DB::table('users')
->whereColumn([
['first_name', '=', 'last_name'],
['updated_at', '>', 'created_at']
])->get();
参数分组
有时你可能需要创建更高级的 where 语句,例如「where exists」或者嵌套的参数分组。Laravel 的查询构造器也能够处理这些。下面有一个括号内的分组约束的示例:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
在上面例子中将 闭包
传递到 orWhere
方法中,指示查询构造器开始一个约束分组。此 闭包
接收一个查询构造器实例,你可以用它来设置应包含在括号组中的约束。上面的例子会产生下面的 SQL:
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
Where Exists 语句
whereExists
方法允许你编写 where exists
SQL 语句。此方法接受一个 闭包
参数,此闭包要接收一个查询构造器实例,让你可以定义放在「exists」语句中的查询:
DB::table('users')
->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
上述的查询将生成以下 SQL:
select * from users
where exists (
select 1 from orders where orders.user_id = users.id
)
JSON where 语句
Laravel 也支持查询 JSON 类型的字段(仅在对 JSON 类型支持的数据库上)。目前,本特性仅支持 MySQL 5.7+ 和 Postgres数据库。可以使用 ->
运算符来查询 JSON 列数据:
$users = DB::table('users')
->where('options->language', 'en')
->get();
$users = DB::table('users')
->where('preferences->dining->meal', 'salad')
->get();
Ordering, Grouping, Limit, & Offset
orderBy
orderBy
方法允许你根据指定字段对查询结果进行排序。orderBy
方法的第一个参数是你想要用来排序的字段,而第二个参数控制排序的方向,可以是 asc
或 desc
:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
latest / oldest
latest
和 oldest
方法允许你轻松地按日期对查询结果排序。默认情况下是对 created_at
字段进行排序。或者,你可以传递你想要排序的字段名称:
$user = DB::table('users')
->latest()
->first();
inRandomOrder
inRandomOrder
方法可以将查询结果随机排序。例如,你可以使用这个方法获取一个随机用户:
$randomUser = DB::table('users')
->inRandomOrder()
->first();
groupBy / having
groupBy
和 having
方法可用来对查询结果进行分组。having
方法的用法和 where
方法类似:
$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();
可以将多个参数传递给 groupBy
方法,按多个字段进行分组:
$users = DB::table('users')
->groupBy('first_name', 'status')
->having('account_id', '>', 100)
->get();
对于更高级的语句,请参阅 havingRaw
方法。
skip / take
可以使用 skip
和 take
方法来限制从查询返回的结果数量或跳过查询中给定数量的结果:
$users = DB::table('users')->skip(10)->take(5)->get();
或者,你也可以使用 limit
和 offset
方法:
$users = DB::table('users')
->offset(10)
->limit(5)
->get();
条件语句
有时你可能想要子句只适用于某个情况为真时才执行查询。例如,如果给定的输入值出现在传入请求中时,你可能想要判断它能达成某个 where
语句,你可以使用 when
方法来完成此操作:
$role = $request->input('role');
$users = DB::table('users')
->when($role, function ($query) use ($role) {
return $query->where('role_id', $role);
})
->get();
只有当 when
方法的第一个参数为 true
时,闭包里的 where
语句才会执行。如果第一个参数是 false
,这个闭包将不会被执行。
你可以将另一个闭包当作第三个参数传递给 when
方法。如果第一个参数的值为 false
时,这个闭包将执行。为了说明如何使用此功能,我们将使用它配置查询的默认排序:
$sortBy = null;
$users = DB::table('users')
->when($sortBy, function ($query) use ($sortBy) {
return $query->orderBy($sortBy);
}, function ($query) {
return $query->orderBy('name');
})
->get();
Inserts
查询构造器也提供了将记录插入数据库表的 insert
方法。insert
方法接受一个字段名和值的数组作为参数:
DB::table('users')->insert(
['email' => 'john@example.com', 'votes' => 0]
);
你还可以在 insert
中传入一个嵌套数组向表中插入多条记录。每个数组代表要插入表中的行:
DB::table('users')->insert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0]
]);
自增 ID
若数据表存在自增的 ID,则可以使用 insertGetId
方法来插入记录然后获取其 ID:
$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);
{note} 当使用 PostgreSQL 时,insertGetId 方法将默认把
id
作为自动递增字段的名称。若你要从不同「顺序」来获取 ID,则可以将字段名称作为第二个参数传递给insertGetId
方法。
Updates
当然,除了在数据库中插入记录外,你也可以使用 update
来更新已存在的记录。update
方法和 insert
方法一样,接受包含要更新的字段及值的数组。你可以使用 where
语句来约束 update
的查询:
DB::table('users')
->where('id', 1)
->update(['votes' => 1]);
更新 JSON 字段
更新 JSON 字段时,应该使用 ->
语法来访问 JSON 对象中的相应键。此操作只能在支持 JSON 字段的数据库上操作:
DB::table('users')
->where('id', 1)
->update(['options->enabled' => true]);
自增 & 自减
查询构造器还为给定字段的递增或递减提供了方便的方法 。此方法提供了一个比手动编写 update
语句更具表达力且更精练的接口。
这两个方法都必须接收至少一个参数——要修改的字段。可以选择传递第二个参数来控制字段递增或递减的量:
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']);
Deletes
查询构造器也可使用 delete
方法从数据表中删除记录。在调用 delete
方法前,还可以通过添加 where
语句来约束 delete
语句:
DB::table('users')->delete();
DB::table('users')->where('votes', '>', 100)->delete();
如果你需要清空表,你可以使用 truncate
方法,这将删除所有行,并重置自动递增 ID 为零:
DB::table('users')->truncate();
悲观锁
查询构造器也包含一些可以帮助你在 select
语句上实现「悲观锁定」的函数 。若要在查询中使用「共享锁」,可以使用 sharedLock
方法。共享锁可以防止选中的行被篡改,直到事务被提交为止:
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
或者,你也可以使用 lockForUpdate
方法。使用「更新」锁可避免行被其它共享锁修改或选取:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
laravel 使用构造器进行增删改查的更多相关文章
- laravel增删改查
基本想法是搭建一个FormController,所有以后需要配置生成后台的controller就继承这个FormController就好了.在FormController中定义属性: class Fo ...
- laravel orm进行增删改查
https://laravelacademy.org/post/9699.html 建议用DB门面直接操作数据库,因为ORM性能低.数据查询上面,ORM不会比DB差的,就比如with,是用了sql最基 ...
- laravel 增删改查 数据库设置 路由设置
laravel 框架的路由设置: url: http://www.shanzezhao.com/laraverl/my_laravel/public/index.php/indexs laravel ...
- 仿联想商城laravel实战---5、无刷新的增删改查(动态页面更新的三种方式(html))
仿联想商城laravel实战---5.无刷新的增删改查(动态页面更新的三种方式(html)) 一.总结 一句话总结: 直接js增加删除修改html 控制器直接返回处理好的页面 用双向绑定插件比如vue ...
- MybatisPlus核心功能——实现CRUD增删改查操作 (包含条件构造器)
CRUD 官方文档:https://baomidou.com/ (建议多看看官方文档,每种功能里面都有讲解)[本文章使用的mybatisplus版本为3.5.2] 条件构造器 一般都是用service ...
- Node.js、express、mongodb 入门(基于easyui datagrid增删改查)
前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...
- (转)SQLite数据库增删改查操作
原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...
- 利用SQLiteOpenHelper创建数据库,进行增删改查操作
Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...
- sqlite数据库操作详细介绍 增删改查,游标
sqlite数据库操作详细介绍 增删改查,游标 本文来源于www.ifyao.com禁止转载!www.ifyao.com Source code package com.example ...
随机推荐
- [POI2004] SZN
Description 给定\(N(N\leq 10000)\)个点的树,要求用最少的路径覆盖树边.路径之间可以有交点,不能有交边.问最少需要几条路径以及在第一问的基础上最长的路径最短是多少? Sol ...
- SPI Flash(W25Q16DV) 基本操作
读取厂家\设备 ID 发送 90H 指令,再发送 00h 的地址,然后接收即可. 代码如下: void SPIFlashReadID(int *pMID, int *pDID) { SPIFlash_ ...
- 浅谈MemCahe
MemCahe 首先介绍下memcahce的定义:是一个分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速度提升效果十分显著. 接下来介绍 ...
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来 ...
- 关于eclipse项目的x号报错的一些问题
有些时候项目中并未有什么问题 但项目前会有一个X号报错且无法运行项目 我们不妨从jre和Tomcat的一些配置中找原因 1,首先查看jre的安装是否正确,可以看见并未出错 ,如果有问题,重新导入一下即 ...
- WINCE 下载地址(转)
WinCE 6.0 安装包比较大,从微软下载时,它只提供一个下载工具,用它下载比较慢在网上查了些资料,把WinCE所需的安装包地址都收集起来了,安装包文件名都是有规律的,可以用迅雷新建批量任务来下载, ...
- 自定义jq插件,鼠标悬浮展示图片或者视频放大图,可自定义展示内容
网站项目经常会遇到一些视频或者图片素材的展示功能,那么就需要自己写一个功能,就是在一些列表页面你想要是这个数据的详细内容,弹框在页面某个位置 例如这是视频悬浮展示效果,可自定义自动播放等属性标签 又例 ...
- CSS3效果:实现气泡效果
首先定义一个 <p class="speech"></p> 先给外层的容器添加样式: p.speech { position: relative; widt ...
- layer.open
1.type-基本层类型 类型:Number,默认:0 layer提供了5种层类型.可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层). 若你采用layer. ...
- 《数据库系统概念》10-ER模型
通过建立实体到概念模型的映射,Entity-Relationship Model可以表达整个数据库的逻辑结构,很多数据库产品都采用E-R模型来表达数据库设计. 一.E-R模型采用了三个基本概念:实体集 ...