13. Laravel 4 Eloquent 基础

创建 Eloquent 模型

class User extends Eloquent {}

注意: 表名将默认为:类名的复数形式并小写。主键将默认为: id

自定义表名(protected $table)
protected $table = 'my_users';
自定义主键(protected $primaryKey)
protected $primaryKey = 'not_id';
关闭自递增(protected $incrementing)
protected $incrementing = false;
关闭 创建时间 与 更新时间 的自动维护(protected $timestamps)
protected $timestamps = false;

注意 在默认情况下需要在表中定义 updated_atcreated_at 字段。如果不希望它们被自动维护,请在模型中设置 $timestamps 属性为 false

开启软删除(protected $softDelete)
protected $softDelete = true;
自定义数据库连接(protected $connection)
protected $connection = 'another';

查询

在查询中临时改变数据库连接(on)
User::on('connection-name')->find(1);
获取所有数据(all|get)
User::all(); // 推荐使用,语义化
User::get();
使用主键检索某一条数据(find)
User::find(1);
获取结果中的第一条数据(first)
User::first();
根据主键获取一条记录或者抛出一个异常(findOrFail|firstOrFail)
User::findOrFail(1);
User::where('votes', '>', 100)->firstOrFail();

注册错误处理器,请监听 ModelNotFoundException:

use Illuminate\Database\Eloquent\ModelNotFoundException;
App::error(function(ModelNotFoundException $e)
{
    return Response::make('Not Found', 404);
});
指定需要获取的字段(pluck)
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)
User::lists('url');

可以通过lists的第二个参数为返回的数组自定义键名:

User::lists('url', 'id');
指定需要获取的行数(take)
User::where('votes', '>', 100)->take(10)->get();
统计(count)
User::where('votes', '>', 100)->count();
自定义 where 语句(whereRaw)
User::whereRaw('age > ? and votes = 100', array(25))->get();
筛选重复(distinct)
User::whereRaw('age > ? and votes = 100', array(25))->distinct()->get();

插入

安全创建
$user = new User;
$user->name = 'John';
$user->save();

集体赋值

白名单(protected $fillable)
protected $fillable = array();
黑名单(protected $guarded)
protected $guarded = array();
阻止所有属性集体赋值
protected $guarded = array('*');
使用模型的 Create 函数
// 常规方法
User::create(array('name' => 'John'));
// 若不存在则创建
User::firstOrCreate(array('name' => 'John'));
// 若不存在则创建,且允许你在 save 之前做其它操作
User::firstOrNew(array('name' => 'John'))->save();

更新

更新一个检索到的模型
$user = User::find(1);
$user->email = 'john@foo.com';
$user->save();
push 操作
$user = User::whereRaw(" name = ? ", array('john'))->get();
$user->push(array('email' => 'john@foo.com'));
$user->save();
仅更新时间戳(touch)
$user->touch();

删除

常规方法
$user = User::find(1);
$user->delete();
$affectedRows = User::where('votes', '>', 100)->delete();
根据主键删除(destroy)
User::destroy(1);
User::destroy(array(1, 2, 3));
User::destroy(1, 2, 3);

软删除

使用条件

模型中开启软删除:

protected $softDelete = true;

表中必须添加 deleted_at 字段:

$table->softDeletes(); // 详见结构生成器 Schema
使用方法

与删除相同,只是数据并非真的删除,而是通过 deleted_at 字段标记。

强制软删除的数据包含到结果集中(withTrashed)
User::withTrashed()->where('account_id', 1)->get();
仅取出软删除的数据(onlyTrashed)
User::onlyTrashed()->where('account_id', 1)->get();
检测一个给定的模型实例是否被软删除(trashed)
if ($user->trashed())
恢复一个已被软删除的记录(restore)
$user->restore();
彻底删除(forceDelete)
$user->forceDelete();

定制的时间戳格式

针对系统的自动维护三字段: created_at updated_at deleted_at

class User extends Eloquent {

    protected function getDateFormat()
    {
        return 'U';
    }

}

查询范围

定义一个查询范围
public function scopePopular($query)
{
    return $query->where('votes', '>', 100);
}
使用一个查询范围
User::popular()->orderBy('created_at')->get();
动态范围

添加参数到您的范围函数:

public function scopeOfType($query, $type)
{
    return $query->whereType($type);
}

然后在范围函数调用中传递参数:

User::ofType('member')->get();

转载于:https://my.oschina.net/5say/blog/188084

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值