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);

软删除

使用条件

模型中开启软删除:url

protected $softDelete = true;

表中必须添加 deleted_at 字段:rest

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

使用方法

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

强制软删除的数据包含到结果集中(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_atorm

class User extends Eloquent {

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

}

查询范围

定义一个查询范围

public function scopePopular($query)
{
    return $query->where('votes', '>', 100);
}

使用一个查询范围

User::popular()->orderBy('created_at')->get();

动态范围

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

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

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

User::ofType('member')->get();
相关文章
相关标签/搜索