Laravel5.3中的原生+查询构造器+Eloquent ORM 常用整理

一。 db门面方法
use Illuminate\Support\Facades\DB;

查:$users = DB::select('select * from users where active = ?', [1]);
更新:DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
插入:DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
删除:$deleted = DB::delete('delete from users’); 无返回值:DB::statement('drop table users’);
事务:DB::beginTransaction(); 你可以通过rollBack方法回滚事务:
DB::rollBack(); 最后,你可以通过commit方法提交事务:
DB::commit();


二 查询构造器,table()  可加多个约束条件
1 查询所有信息 $users = DB::table('users')->get();

2 查询name为John的第一行 $user = DB::table('users')->where('name', 'John')->first();
3 查询name为John的列value为email的集合 $email = DB::table('users')->where('name', 'John')->value('email');

4获取单列title的列集合  $titles = DB::table('roles')->pluck('title');
5 聚合函数 count max min avg sum 
$users = DB::table('users')->count(); $price = DB::table('orders')->max('price');

6 -》distinct()
7 where的用法 -》where(’xxx’,’符号’,’值’);
8 ->orderby(‘列名’,’desc’);
9  插入
DB::table('users')->insert([     ['email' => 'taylor@example.com', 'votes' => 0],     ['email' => 'dayle@example.com', 'votes' => 0] ]);

10可以反回自增id    $id = DB::table('users')->insertGetId(     ['email' => 'john@example.com', 'votes' => 0] );

11 更新 DB::table('users')             ->where('id', 1)             ->update(['votes' => 1]);


12更新数字  默认为1 decrement DB::table('users')->increment('votes', 5);
13更新数字加字段组合 DB::table('users')->increment('votes', 1, ['name' => 'John']);

14 删除 DB::table('users')->delete(); DB::table('users')->where('votes', '>', 100)->delete();


三 Eloquent ORM  对象关系映射(Object Relational Mapper)工具
模型中:use Illuminate\Database\Eloquent\Model

 这里在模型中还可以做如下的设置:

设置关联模型的表 protected $table = ‘my_flights’;
设置是否表中有create_at 和update_at public $timestamps = fasle;
设置日期存储格式 protected $dateFormat = 'U';


控制器中:书写orm  use App\Flight   as flight;(敲黑板)

1  获取 flight模型关联表的所有数据 ——all方法返回所有结果

$flights = flight::all();
$flights = flight::where(‘id’,’148’)->get(); //获取id为148 的所有列  $flights = flight::orderby(’time’,’desc’)->get();//获取所有列,按照time字段倒序排序
orm本质也是查询构造器,所以可以使用查询构造器中所有的方法  ,不一一列举
2 没找到就报错:findorfail
$list = article::find(148);   返回null
$list = article::findorFail(148);   返回404 


插入a $Article = new article;
        $Article->title = '张帆';
        $Article->save();

创建模型实例,设置对应的字段属性。save 
插入b  怎么突然开车了 = =。 $article = article::create(['title' => '何国龙']);
这样是不行的,需要在model里面声明一下可以批量操作的字段。fillable就像是白名单 protected $fillable = [’title’]; 并且这样会返回插入的实例 。 对应的黑名单就是 guarded 。 可以 protected $guraded = []; 来快速设置所有字段都可以自由插入。
插入c   $flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);
如果不存在name为flight 10 的记录则插入 。
4 更新 $article = article::find(3);         $article->title = 'xxx';         $article->save(); 创建模型实例,这里与插入不同的是,需要先获取想要更新的地方的模型。作为对象。然后进行更新。

article::where('title', 'xxx')             ->where('user_id', '148')             ->update(['body' => 185]);
更新title为xxx 且 user_id为148 的记录 body更新为185 

5  删除a $article = article::find(7);         $article->delete();

删除b article::destroy(5);

删除c $deletedRows = App\Flight::where('active', 0)->delete();




总结:
DB适合用于对性能要求高或者业务逻辑简单的项目,ORM适合业务逻辑比较复杂的项目。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃鸳鸯锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值