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);* ...
随机推荐
- ubuntu添加桌面或launcher快捷方式
以eclipse为例,自行下载的. 创建文件/usr/share/applications/eclipse-kepler.desktop 文件内容: #------------------------ ...
- QQ在线客服JS代码,自适应漂浮在网页右侧
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- python 使用字符串名调用类以及调用类方法名
在python中,有时调用者仅知道类名和类方法,不负责实际的函数调用,而是将要调用的类名和类方法告诉一个中间函数,由中间函数负责实际调用函数.中间函数需以被告知的字符串调用类和类方法. ...
- Cauchy 级数浓缩判别法
- js 变量提升和函数提升原理
关于js的变量,开始的时候我们都会被告知,变量声明应该在引用该变量之前.关于为什么要这样做呢,开始的时候本着会用就行的目的,也没去深究.不过后来经常会发现一些让人很费解的..姑且称为现象吧.先看一段代 ...
- MyEclipse快捷键敏感设置
对于一个程序员来说,敲代码没有快捷键是很难受的.自从我装了MyEclipse之后发现快捷键敏感性太差了比如说我打输出语句System.out.println();一般打syso就会有提示,但是我的My ...
- css3 2D动画的基本用法和介绍
<style> body{height:400px;border:1px solid #000;} .box{width:90px;height:30px;border:1px solid ...
- Activity设置全屏的三种方法
1.super.onCreate(savedInstanceState)方法之前调用: setTheme(android.R.style.Theme_Light_NoTitleB ...
- c++中的指针之指针在数组
使用一维指针数组输出一维数组中的数 int array[]={1,2,3,4,5,6}; int *p; p=array; for(int i=0;i<6;i++){ ...
- MFC 关于如何实现浏览文件
在做界面的时候,如果涉及到文件,就要输入文件的地址 第一种方法是直接输入,嗯...╮( ̄▽ ̄)╭ 第二种方法就是常见的浏览,然后选择文件<( ̄︶ ̄)/ 其代码如下: CString filena ...