当前位置:Gxlcms > PHP教程 > laravel常用查询

laravel常用查询

时间:2021-07-01 10:21:17 帮助过:32人阅读

手册写得比较简单,不足以应付我们日常可能用到的查询:

$oSpecialty = new Specialty();//dd(Specialty::where('id', 3)->value('path'));//查询单个字段,返回的是string//dd(Specialty::where('id',3)->first(['id','pid','path']));//查询多个定义的字段//dd(Specialty::where('pid',0)->get(['id','pid','path']));//查询多个字段组成的数据集//dd(Specialty::where('pid',0)->lists('id'));//查询某个列的集合,返回结果key是0,1,2值为id的集合dd(Specialty::where('pid',0)->skip(5)->take(10)->get());//查询限制条数的记录//dd(DB::update('update users set name="neo2" where id=1200'));//原生态更新

另外的例子:

//$userModel = new User();$info1=User::where('id', 1)->get();$info2=User::find(1);$info3=User::whereid(1)->first();$info4=User::where('id', 1)->first();$info5 = User::where(['id'=>1,'email'=>'admin@163.com'])->first();//多个条件$info6 = User::whereid(1)->first();//采用where+键名查询$info7 = User::where(['id'=>1])->first();$info8 = User::where(['id'=>1])->first()->toArray();//注意toArray对象为null会报错,因此一般不这样用$info9 = UserBind::where(['fromId'=>1])->get()->toArray();//注意toArray对象为null会报错,因此一般不这样用

更多例子

User::whereIn('id', $id)->delete();//需要注意$id为数组,不是用逗号链接的字符串Teacher::whereIn('user_id',$id)->delete();;//删除老师表

人气教程排行