laravel Eloquent ORM 文档:https://learnku.com/docs/laravel/5.7/eloquent/2294
- 什么是ORM?
ORM,即 Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在操作具体的 业务对象时,就不需要再去和复杂的SQL语句打交道,只需简单的操作对象的属性和方法即可。
ORM 两种最常见的实现方式是 ActiveRecord 和 DataMapper
ActiveRecord(非常流行) 中模型与数据表一一对应,
DataMapper 中模型与数据表是完全分离的。
Laravel 的 Eloquent ORM 使用 ActiveRecord 实现方式,每一个 Eloquent 模型类对应着数据库中的一张表,我们通过调用模型类的相应方法实现对数据库的增删改查。
由于 Eloquent 模型是查询构造器,你可在 Eloquent 查找中使用查询构造器的任何方法。
- 简单的来说
就是laravel框架内提供的一个实体关系映射框架(ORM),名字叫eloquent
是一个用于关系型数据库实体关系映射的组件,laravel内置的,可以以操作对象的方式操作数据库的数据变更和查询
以上的内容估计您也能对ORM进行一个了解了
与数据库的映射关系
类名 <------------> 表名
属性 <------------>字段
属性的约束 <--------->字段的类型
实例对象 <-------> 表记录
上面的白话是借鉴他的点击这里查看
- 简单总结一下orm操作数据库的方法
拿users表来说(User是model)
User::find(1) 查找单条数据
User::all() 查找所有数据
User::find(1)->delete() 删除单条数据
User::destory(array(1,2,3)) 删除单条或多条数据
User::save() 保存数据(我一般修改都用他)
也可以增加修改放一个接口用它:
$user= new User();
$user->fill($data)->save();
User::first() 取第一条数据
User::where('name', '=', '***')->update(array('pwd' => '**')); 指定查询条件,更新数据
User::truncate() 清空数据表,危险操作
User::where('name', '=', '**')->get(array('id','pwd')); 配合查询条件获取多条数据
User::pluck('name'); 返回表中该字段的第一条记录(这个确实很好用)
User::lists('name'); 返回一列数据
User::where('name', '=', '***')->toSql(); 获取查询的sql语句,仅用于条件,不能用户带get()之类的带查询结果的查询中(我一般打印sql的时候用的是:DB::connection()->enableQueryLog(); // 开启QueryLog,最后写dd(DB::getQueryLog());查看这样我感觉全部sql都有也方便)
条件查询的方法很多比如:
1.最普通的条件查询 User::where('字段名','查询字符','限制条件') 例:User::where('name', 'LIKE', '%...%').
2.多条件查询,使用多个where User::where('name', 'LIKE', '...%')->where('pwd', '=', '123')->get();或查询操作使用orWhere(),使用方法通where.
3.直接用sql语句写查询条件 User::whereRaw('name= ? and pwdLIKE ?', array('123', '%...%')).
4.其他查询方法:
whereIn(),whereBetween(),whereNested()子查询,orWhereNested(),whereNotIn(),whereNull(),whereNotNull()
5.快捷方式 whereName('123') 查询'name' = '123'的数据,默认系统无此方法需要字段定义在model上(如:照片图1),name为字段名称
6.结果排序:
使用order关键字:
User::where('name', '=', '123')->orderBy('id')->get(); 默认asc(正序)
orderBy('id', 'desc')(倒叙)
限制结果数量
take()方法
User::take(2)->get(); //select * from `users` limit 2
指定偏移
User::take(2)->skip(2)->get(); //select * from `users` limit 2 offset 2
7.分页:
User::where('name','=','123')->paginate(3) //每页显示5条
8.倆表或者多表联查(要有关联在能查)
模型关联这里放这自己看:(https://learnku.com/docs/laravel/6.x/eloquent-relationships/5177)
public function a()
{
return $this->belongsTo('App\Models\a', 'user_id','id');
}
public function b()
{
return $this->belongsTo('App\Models\b', 'a_id','aid');
}
$model = $this->whereHas('a', function ($query) use ($search) {
$query->orwhere('name', 'like', '%' . $search['name'] . '%');
})->whereHas('b', function ($query) use ($search) {
$query->orwhere('p_phone', 'like', '%' . $search['name'] . '%');
})->paginate(3);//这个呢就是一个输入框里面输入手机号啊姓名啊都成查到所以用orwhere
详情的话就简单了:
public function show(User $user)
{
$user->a->b;
return $user;这就关联好了全部查处
}
基本上就这样以后在补点
图1:
php artisan ide-helper:generate - 为 Facades 生成注释
php artisan ide-helper:models - 为数据模型生成注释
php artisan ide-helper:meta - 生成 PhpStorm Meta file
//自增
//
n
u
m
=
D
B
:
:
t
a
b
l
e
(
′
s
t
u
d
e
n
t
′
)
−
>
i
n
c
r
e
m
e
n
t
(
′
a
g
e
′
)
;
/
/
num = DB::table('student')->increment('age'); //
num=DB::table(′student′)−>increment(′age′);//num = DB::table(‘student’)->increment(‘age’,3);
//自减
//$num = DB::table(‘student’)->decrement(‘age’,3);
//自增的同时修改其他的参数
$num = DB::table(‘student’)
->where(‘id’,1004)
->increment(‘age’,1,[‘name’=>‘iimooc’]);
@2 truncate//很危险一般不建议用TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。
正因为上述原理,如果有ROLLBACK语句,DELETE操作将被撤销,但TRUNCATE不会撤销。
//DB::table(‘student’)->truncate();
//这个操作不返回任何的东西
@4 pluck()
//pluck返回结果集中的指定字段
//
n
a
m
e
=
D
B
:
:
t
a
b
l
e
(
′
s
t
u
d
e
n
t
′
)
/
/
−
>
p
l
u
c
k
(
′
n
a
m
e
′
)
;
@
5
l
i
s
t
s
(
)
/
/
l
i
s
t
s
可
以
指
定
返
回
某
个
键
作
为
下
标
/
/
name = DB::table('student') //->pluck('name'); @5 lists() //lists 可以指定返回某个键作为下标 //
name=DB::table(′student′)//−>pluck(′name′);@5lists()//lists可以指定返回某个键作为下标//name = DB::table(‘student’)
//->lists(‘name’,‘id’);
@6 select()
//select
//
s
t
u
d
e
n
t
s
=
D
B
:
:
t
a
b
l
e
(
′
s
t
u
d
e
n
t
′
)
/
/
−
>
s
e
l
e
c
t
(
′
i
d
′
,
′
n
a
m
e
′
,
′
a
g
e
′
)
/
/
−
>
g
e
t
(
)
;
@
7
c
h
u
n
k
(
)
/
/
c
h
u
n
k
分
段
进
行
获
取
,
真
实
项
目
中
会
一
次
插
入
一
千
条
/
/
可
以
加
入
r
e
t
u
r
n
f
a
l
s
e
让
语
句
进
行
停
止
e
c
h
o
′
<
p
r
e
>
′
;
D
B
:
:
t
a
b
l
e
(
′
s
t
u
d
e
n
t
′
)
−
>
c
h
u
n
k
(
2
,
f
u
n
c
t
i
o
n
(
students = DB::table('student') //->select('id','name','age') //->get(); @7 chunk() //chunk分段进行获取,真实项目中会一次插入一千条 //可以加入return false让语句进行停止 echo '<pre>'; DB::table('student')->chunk(2,function(
students=DB::table(′student′)//−>select(′id′,′name′,′age′)//−>get();@7chunk()//chunk分段进行获取,真实项目中会一次插入一千条//可以加入returnfalse让语句进行停止echo′<pre>′;DB::table(′student′)−>chunk(2,function(students){
var_dump($students);
//一般配合if条件使用
//return false;
});
2.5查询构造器中的聚合函数
@1 count()
n
u
m
=
D
B
:
:
t
a
b
l
e
(
′
s
t
u
d
e
n
t
′
)
−
>
c
o
u
n
t
(
)
;
@
2
m
a
x
(
)
num = DB::table('student')->count(); @2 max()
num=DB::table(′student′)−>count();@2max()max = DB::table(‘student’)->max(‘age’);
@3 min()
m
i
n
=
D
B
:
:
t
a
b
l
e
(
′
s
t
u
d
e
n
t
′
)
−
>
m
i
n
(
′
a
g
e
′
)
;
@
4
a
v
g
(
)
min = DB::table('student')->min('age'); @4 avg()
min=DB::table(′student′)−>min(′age′);@4avg()avg = DB::table(‘student’)->avg(‘age’);
@5 sum()$sum = DB::table(‘student’)->sum(‘age’);
//findOrFail()根据条件查找,没有找到就报错
//$student = Student::findOrFail(1006);
Model中的修改
//指定允许批量赋值的字段
protected $fillable = [‘name’,‘age’];
//指定不允许批量赋值的字段
protected $guarded = [];
//关闭显示创建和修改时间(自动维护时间戳)一般打开
public
t
i
m
e
s
t
a
m
p
s
=
t
r
u
e
;
/
/
让
时
间
显
示
成
为
u
n
i
x
时
间
戳
p
r
o
t
e
c
t
e
d
f
u
n
c
t
i
o
n
g
e
t
D
a
t
e
F
o
r
m
a
t
(
)
r
e
t
u
r
n
t
i
m
e
(
)
;
p
r
o
t
e
c
t
e
d
f
u
n
c
t
i
o
n
a
s
D
a
t
e
T
i
m
e
(
timestamps = true; //让时间显示成为unix时间戳 protected function getDateFormat() { return time(); } protected function asDateTime(
timestamps=true;//让时间显示成为unix时间戳protectedfunctiongetDateFormat()returntime();protectedfunctionasDateTime(val)
{
return $val;
}
//firstOrcreate()以属性查找,若没有则新增实例
//$student = Student::firstOrcreate(
// [‘name’=>‘imoocs’]
// );
//firstOrNew()以属性查找,若没有则新增实例.需要保存的话自己调用save()
$student = Student::firstOrNew(
[‘name’=>‘imoocss’]
);
$bool = $student->save();
@2 通过主键值删除
//删除单个
//
n
u
m
=
S
t
u
d
e
n
t
:
:
d
e
s
t
r
o
y
(
1007
)
;
/
/
删
除
多
个
/
/
num = Student::destroy(1007); //删除多个 //
num=Student::destroy(1007);//删除多个//num = Student::destroy(1008,1009);
//也可以写在数组中
//
n
u
m
=
S
t
u
d
e
n
t
:
:
d
e
s
t
r
o
y
(
[
1004
,
1005
]
)
;
/
/
v
a
r
d
u
m
p
(
num = Student::destroy([1004,1005]); //var_dump(
num=Student::destroy([1004,1005]);//vardump(num);