php Laravel框架学习(二) 之 Eloquent 基础
本文重点介绍 Laravel 框架的 Eloquent 基础 知识!
创建 Eloquent 模型
1
|
class
User
extends
Eloquent {}
|
注意: 表名将默认为:类名的复数形式并小写。主键将默认为: id
。
自定义表名(protected $table)
1
|
protected
$table =
'my_users'
;
|
自定义主键(protected $primaryKey)
1
|
protected
$primaryKey =
'not_id'
;
|
关闭自递增(protected $incrementing)
1
|
protected
$incrementing =
false
;
|
关闭 创建时间 与 更新时间 的自动维护(protected $timestamps)
1
|
protected
$timestamps =
false
;
|
注意 在默认情况下需要在表中定义 updated_at
和 created_at
字段。如果不希望它们被自动维护,请在模型中设置 $timestamps
属性为 false
。
开启软删除(protected $softDelete)
1
|
protected
$softDelete =
true
;
|
自定义数据库连接(protected $connection)
1
|
protected
$connection =
'another'
;
|
查询
在查询中临时改变数据库连接(on)
1
|
User::on(
'connection-name'
)->find(
1
);
|
获取所有数据(all|get)
1
2
|
User::all();
// 推荐使用,语义化
User::get();
|
使用主键检索某一条数据(find)
1
|
User::find(
1
);
|
获取结果中的第一条数据(first)
1
|
User::first();
|
根据主键获取一条记录或者抛出一个异常(findOrFail|firstOrFail)
1
2
|
User::findOrFail(
1
);
User::where(
'votes'
,
'>'
,
100
)->firstOrFail();
|
注册错误处理器,请监听 ModelNotFoundException:
1
2
3
4
5
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
return
Response::make(
'Not Found'
,
404
);
});
|
指定需要获取的字段(pluck)
1
2
3
4
5
6
|
User::all(array(
'url'
));
User::get(array(
'url'
));
User::find(
1
, array(
'url'
));
User::find(
1
)->pluck(
'url'
);
User::first(array(
'url'
));
User::first()->pluck(
'url'
);
|
列表形式获取表中的某个字段值(lists)
1
|
User::lists(
'url'
);
|
可以通过lists的第二个参数为返回的数组自定义键名:
1
|
User::lists(
'url'
,
'id'
);
|
指定需要获取的行数(take)
1
|
User::where(
'votes'
,
'>'
,
100
)->take(
10
)->get();
|
统计(count)
1
|
User::where(
'votes'
,
'>'
,
100
)->count();
|
自定义 where 语句(whereRaw)
1
|
User::whereRaw(
'age > ? and votes = 100'
, array(
25
))->get();
|
筛选重复(distinct)
1
|
User::whereRaw(
'age > ? and votes = 100'
, array(
25
))->distinct()->get();
|
插入
安全创建
1
2
3
|
$user =
new
User;
$user->name =
'John'
;
$user->save();
|
集体赋值
白名单(protected $fillable)
1
|
protected
$fillable = array();
|
黑名单(protected $guarded)
1
|
protected
$guarded = array();
|
阻止所有属性集体赋值
1
|
protected
$guarded = array(
'*'
);
|
使用模型的 Create 函数
1
2
3
4
5
6
|
// 常规方法
User::create(array(
'name'
=>
'John'
));
// 若不存在则创建
User::firstOrCreate(array(
'name'
=>
'John'
));
// 若不存在则创建,且允许你在 save 之前做其它操作
User::firstOrNew(array(
'name'
=>
'John'
))->save();
|
更新
更新一个检索到的模型
1
2
3
|
$user = User::find(
1
);
$user->email =
'john@foo.com'
;
$user->save();
|
push 操作
1
2
3
|
$user = User::whereRaw(
" name = ? "
, array(
'john'
))->get();
$user->push(array(
'email'
=>
'john@foo.com'
));
$user->save();
|
仅更新时间戳(touch)
1
|
$user->touch();
|
删除
常规方法
1
2
3
|
$user = User::find(
1
);
$user->delete();
$affectedRows = User::where(
'votes'
,
'>'
,
100
)->delete();
|
根据主键删除(destroy)
1
2
3
|
User::destroy(
1
);
User::destroy(array(
1
,
2
,
3
));
User::destroy(
1
,
2
,
3
);
|
软删除
使用条件
模型中开启软删除:
1
|
protected
$softDelete =
true
;
|
表中必须添加 deleted_at
字段:
1
|
$table->softDeletes();
// 详见结构生成器 Schema
|
使用方法
与删除相同,只是数据并非真的删除,而是通过 deleted_at
字段标记。
强制软删除的数据包含到结果集中(withTrashed)
1
|
User::withTrashed()->where(
'account_id'
,
1
)->get();
|
仅取出软删除的数据(onlyTrashed)
1
|
User::onlyTrashed()->where(
'account_id'
,
1
)->get();
|
检测一个给定的模型实例是否被软删除(trashed)
1
|
if
($user->trashed())
|
恢复一个已被软删除的记录(restore)
1
|
$user->restore();
|
彻底删除(forceDelete)
1
|
$user->forceDelete();
|
定制的时间戳格式
针对系统的自动维护三字段: created_at
updated_at
deleted_at
1
2
3
4
5
6
7
8
|
class
User
extends
Eloquent {
protected
function getDateFormat()
{
return
'U'
;
}
}
|
查询范围
定义一个查询范围
1
2
3
4
|
public
function scopePopular($query)
{
return
$query->where(
'votes'
,
'>'
,
100
);
}
|
使用一个查询范围
1
|
User::popular()->orderBy(
'created_at'
)->get();
|
动态范围
添加参数到您的范围函数:
1
2
3
4
|
public
function scopeOfType($query, $type)
{
return
$query->whereType($type);
}
|
然后在范围函数调用中传递参数:
1
|
User::ofType(
'member'
)->get();
|