laravel常用查询

WBOY
Release: 2016-06-20 12:32:39
Original
1680 people have browsed it

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

$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'));//原生态更新
Copy after login

另外的例子:

//$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会报错,因此一般不这样用
Copy after login

更多例子

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!