基本操作

新增

$user = new User;
$user->name = 'John';
$user->save();
$insertedId = $user->id;//从对象取得 id 属性值

使用模型的 Create 方法

class User extends Model {
protected $guarded = ['id', 'account_id'];//黑名单,不会被更新
} // 在数据库中建立一个新的用户...
$user = User::create(['name' => 'John']); // 以属性找用户,若没有则新增并取得新的实例...
$user = User::firstOrCreate(['name' => 'John']); // 以属性找用户,若没有则建立新的实例...
$user = User::firstOrNew(['name' => 'John']);

删除

$this->where($where)->delete();

或者
$user = User::find(1);
$user->delete();

更新

return $this->where($where)->update($data);

或者
$user = User::find(1);
$user->update($data);

查找

//取出所有记录,all()得出的是对象集合,可以遍历
$this->all()->toArray(); //根据主键取出一条数据
$one = $this->find('2');
return array(
$one->id,
$one->title,
$one->content,
); //查找id=2的第一条数据
$this->where('id', 2)->first()->toArray(); //查找id>0的所有数据
$this->where('id', '>', '0')->get()->toArray(); //查找id>0的所有数据,降序排列
$this->where('id', '>', '0')->orderBy('id', 'desc')->get()->toArray(); //查找id>0的所有数据,降序排列,计数
$this->where('id', '>', '0')->orderBy('id', 'desc')->count(); //offset,limit
$this->where('id', '>', '0')->orderBy($order[0], $order[1])->skip($offset)->take($limit); //等同于
$this->where('id', '>', '0')->orderBy($order[0], $order[1])->offset($offset)->limit($limit);

更多:

//条件类:

where('id', '>', '0')
where('id', '>=', '0')
where('id', '<', '0')
where('id', '<=', '0')
where('id', 'like', 'name%') whereIn($key, $array)
whereNotIn($key, $array)
whereBetween($key, $array)
whereNotBetween($key, $array) orWhereIn($key, $array)
orWhereNotIn($key, $array)
orWhereBetween($key, $array)
orWhereNotBetween($key, $array) //结果方法:Illuminate\Database\Query\Builder first()取第一个
get()取所有
all()取所有(无条件) //聚合方法
count()统计
avg()求平均值
sum()
max()
min()

Eloquent ORM - Laravel 中文文档

http://laravel-china.org/docs/5.0/eloquent

Eloquent ORM笔记的更多相关文章

  1. Eloquent ORM 学习笔记

    最近在学习Laravel,觉得ORM功能很强大,我这里只是简单探索了一点,如果有更好的笔记,还请分享. 因为重点在于Eloquent ORM,所以路由设置,控制器就不详细描述了,这里直接进入Model ...

  2. laravel5.5源码笔记(八、Eloquent ORM)

    上一篇写到Eloquent ORM的基类Builder类,这次就来看一下这些方便的ORM方法是如何转换成sql语句运行的. 首先还是进入\vendor\laravel\framework\src\Il ...

  3. Eloquent ORM 之关联查询

    小伙伴们好,本文是在我的前一篇随笔的基础上完成的,还没有浏览的同学,请移尊驾哦 Eloquent ORM学习笔记. 前一篇文章用到了leftJoin方法,其实Eloquent对于模块之间的关联查询有自 ...

  4. Laravel Eloquent ORM

    Eloquent ORM 简介 基本用法 集体赋值 插入.更新.删除 软删除 时间戳 查询范围 关系 查询关系 预先加载 插入相关模型 触发父模型时间戳 与数据透视表工作 集合 访问器和调整器 日期调 ...

  5. [转]Laravel 4之Eloquent ORM

    Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...

  6. Laravel 数据库操作 Eloquent ORM

    laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...

  7. [Laravel] 03 - DB facade, Query builder & Eloquent ORM

    连接数据库 一.Outline 三种操作数据库的方式. 二.Facade(外观)模式 Ref: 解读Laravel,看PHP如何实现Facade? Facade本质上是一个“把工作推给别人做的”的类. ...

  8. laravel 5.1 使用Eloquent ORM 操作实例

    Laravel 的 Eloquent ORM 提供了更优雅的ActiveRecord 实现来和数据库的互动. 每个数据库表对应一个模型文件. 数据库配置 .env文件(也可以直接修改config/da ...

  9. laravel通过Eloquent ORM实现CURD

    //Eloquent ORM public function orm1() { //all(); 返回所有数据: /*$students=Student::all(); dd($students);* ...

随机推荐

  1. 原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))

    场景: 客户端初始化正常,到IIS服务就报80040154. System.Web.Services.Protocols.SoapException: System.Web.Services.Prot ...

  2. 如何使用NodeJs来监听文件变化

    1.前言 在我们调试修改代码的时候,每修改一次代码,哪怕只是很小的修改,我们都需要手动重新build文件,然后再运行代码,看修改的效果,这样的效率特别低,对于开发者来说简直不能忍. 2.构建自动编译工 ...

  3. install mysql using binary and configure manu

    (1)下载,解压 (2)初始化数据库 ./scripts/mysql_install_db --defaults-file=../my.cnf --user=guofeng (3)启动命令 ./bin ...

  4. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  5. Winodow Server Backup学习向导-window 2008

    1.安装Window Server Backup 2.备份服务器 3.恢复服务器 4.优化和备份服务器性能 Windows Server Backup 中的新增功能有哪些? Windows Serve ...

  6. Changing the Color of Linux ls Command 改变Linux的ls命令显示的颜色

    Linux command ls basically use the file /etc/DIR_COLORS or /etc/DIR_COLORS.xterm to define the color ...

  7. C# 直接调用vs 委托vs动态调用vs动态类型vs反射,最佳性能测试

    懒得解释,自己看代码 测试结果: Direct call:00:00:00.0742191Delegate Direct:00:00:00.0687487Method Factory(IL):00:0 ...

  8. 关于原生JS获取类相关的代码

    <script> var FungetElementsByClassName = function(str,root,tag){ if(root){ root = typeof root ...

  9. uva 11174 Stand in a Line

    // uva 11174 Stand in a Line // // 题目大意: // // 村子有n个村民,有多少种方法,使村民排成一条线 // 使得没有人站在他父亲的前面. // // 解题思路: ...

  10. iOS之NSString类中compare方法的陷阱

    typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDe ...