首页 技术 正文
技术 2022年11月20日
0 收藏 640 点赞 3,328 浏览 1479 个字
一.DB门面
1.insert
DB::insert('insert into table(`name`) value(?)', ['test']);2.update
DB::update('update into table set name=? where id=?', ['test', 10]);3.delete
DB::delete('delete from tb where id=?', [1]);4.select
DB:select('select * from tb');二.查询构建器(使用查询构建器不会触发模型事件)
1.insert
DB::table('tb')->insert(['name' => 'test']);2.update
DB::table('tb')->where('id', 1)->update(['name' => 'test']);3.delete
DB::table('tb')->where('id', 1)->delete();4.select
# 多条
DB::table('tb')->where('cat', 1)->orWhere(function($query){
return $query->where('vote', '>', 1);
})->orderBy('id', 'DESC')->select('name')->skip(5)->take(10)->get();#一条
DB::table('tb')->where('cat', 1)->first();#一列
DB::table('tb')->where('cat', 1)->value('col');
DB::table('tb')->where('cat', 1)->pluck('col');三.Eloquent ORM(本身就是查询构建器)
1.insert(也可以使用insert方法插入一个数组到数据库,但不会触发事件)$model = new TbModel;
$model->name = 'test';
$model->save();使用create,但需要模型限定fillable或guarded
TbModel::create(['name' => 'test']);create和save的区别是
a.create的参数接受的一个字段数组,save也可以接受一个数组,但是只是用来指定timestamps的值
b.create返回的是一个model,save只返回true或false2.update
$model = TbModel::first(1);
$model->name = 'test';
$model->save();带where并且只更新指定字段,和查询构建器一样
$model = TbModel::first(1);
$model->where('time', today())->update(['delayed'=>1]);save无法和where共用,它是根据主键来保存的;保存受影响的字段;3.delete
TbModel::first(1)->delete();
TbModel::destory(1);
Flight::where('id', 1)->delete();4.select
#多条
TbModel::all();//不能带where
TbModel::where('cat', 1)->get(); //可以带where#单条
TbModel::find(1); // 利用主键取回
如果查询条件带where,而且不是主键,则使用first
TbModel::where('time', today())->first();
  

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901