Eloquent ORM笔记
基本操作
新增
$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笔记的更多相关文章
- Eloquent ORM 学习笔记
最近在学习Laravel,觉得ORM功能很强大,我这里只是简单探索了一点,如果有更好的笔记,还请分享. 因为重点在于Eloquent ORM,所以路由设置,控制器就不详细描述了,这里直接进入Model ...
- laravel5.5源码笔记(八、Eloquent ORM)
上一篇写到Eloquent ORM的基类Builder类,这次就来看一下这些方便的ORM方法是如何转换成sql语句运行的. 首先还是进入\vendor\laravel\framework\src\Il ...
- Eloquent ORM 之关联查询
小伙伴们好,本文是在我的前一篇随笔的基础上完成的,还没有浏览的同学,请移尊驾哦 Eloquent ORM学习笔记. 前一篇文章用到了leftJoin方法,其实Eloquent对于模块之间的关联查询有自 ...
- Laravel Eloquent ORM
Eloquent ORM 简介 基本用法 集体赋值 插入.更新.删除 软删除 时间戳 查询范围 关系 查询关系 预先加载 插入相关模型 触发父模型时间戳 与数据透视表工作 集合 访问器和调整器 日期调 ...
- [转]Laravel 4之Eloquent ORM
Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...
- Laravel 数据库操作 Eloquent ORM
laravel 操作数据库一般都使用它的Eloquent ORM才操作 建立模型 <?php namespace App; use Illuminate\Database\Eloquent\Mo ...
- [Laravel] 03 - DB facade, Query builder & Eloquent ORM
连接数据库 一.Outline 三种操作数据库的方式. 二.Facade(外观)模式 Ref: 解读Laravel,看PHP如何实现Facade? Facade本质上是一个“把工作推给别人做的”的类. ...
- laravel 5.1 使用Eloquent ORM 操作实例
Laravel 的 Eloquent ORM 提供了更优雅的ActiveRecord 实现来和数据库的互动. 每个数据库表对应一个模型文件. 数据库配置 .env文件(也可以直接修改config/da ...
- laravel通过Eloquent ORM实现CURD
//Eloquent ORM public function orm1() { //all(); 返回所有数据: /*$students=Student::all(); dd($students);* ...
随机推荐
- elasticsearch-查询
使用如下语句创建一个名字为:user_document,别名为user的索引 PUT:http://localhost:9200/user_document { "settings" ...
- Docker ntpdate Permition error
After building a Dockerfile, I run it. I figure out that there is something wrong with local time. S ...
- GCD in Swfit 3.0
这里包括了Queue, Group, Barrier, Semaphore等内容.基本上常用的GCD对象和方法在Swift3.0的改变都囊括其中. 代码在这里:https://github.com/f ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- Log4j的ConversionPattern无缝适配到Logback
为了能将log4j的ConversionPattern无缝应用到logback上来,需要对两个Conversion做适配,具体可以参考:Log4j 与 Logback的ConversionPatter ...
- DOM基础
DOM(Document Object Model)即文档对象模型,针对HTML 和XML 文档的API(应用程序接口).DOM 描绘了一个层次化的节点树,运行开发人员添加.移除和修改页面的某一部分. ...
- Object-C内存管理的理解总结
今天看到了OC的内存管理这块,觉得很亲切. 自己的习惯是尽量自己掌控程序的空间和时间,有点强迫症的感觉.用C和C++做项目的时候,时时刻刻都在操心这new和delete的配对使用和计数,学习stl和b ...
- [转]彻底征服Word 2007标题多级列表
[转]彻底征服Word 2007标题多级列表 用Word编写文档的人都知道,一篇长文档一般是需要分章节来划分段落的.在Word中也有对应的工具来完成这项任务,这就是多级列表.然而绝大多数使用Micro ...
- Java Servlet与Web容器之间的关系
自从计算机软件开发进入网络时代,就开始涉及到通讯问题.在客户/服务器(也叫C/S应用)时期,每个软件都有自己的客户端和服务器端软件.并且客户端和服务器端之间的通讯协议差别也很大.后来随着互联网的发展, ...
- Install Hbase
1. You should guarantee you have installed hadoop on your computers. 2. Download and uncompress the ...