Laravel 5框架学习之Eloquent (laravel 的ORM)


Posted in PHP onApril 08, 2015

我们来生成第一个模型

php artisan make:model Article

#输出

Model created successfully.

Created Migration: 2015_03_28_062517_create_articles_table

查看一下生成的文件 app/Article.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

 //

}

没什么特别的,除了继承自 Model 以外,但是具有强大的功能,这些都封装在laravel的Model中。模型自动具有了 save() update() findXXX() 等强大的功能。

tinker 是 laravel提供的命令行工具,可以和项目进行交互。

php artisan tinker

#以下是在tinker中的交互输入
Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman
>>> $name = 'zhang jinglin';
=> "zhang jinglin"

>>> $name
=> "zhang jinglin"

>>> $article = new App\Article;
=> <App\Article #000000005c4b7ee400000000ab91a676> {}

>>> $article->title = 'My First Article';
=> "My First Article"

>>> $article->body = 'Some content...';
=> "Some content..."

>>> $article->published_at = Carbon\Carbon::now();
=> <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> {
    date: "2015-03-28 06:37:22",
    timezone_type: 3,
    timezone: "UTC"
  }

>>> $article;
=> <App\Article #000000005c4b7ee400000000ab91a676> {
    title: "My First Article",
    body: "Some content...",
    published_at: <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> {
      date: "2015-03-28 06:37:22",
      timezone_type: 3,
      timezone: "UTC"
    }
  }

>>> $article->toArray();
=> [
    "title"    => "My First Article",
    "body"     => "Some content...",
    "published_at" => <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> {
      date: "2015-03-28 06:37:22",
      timezone_type: 3,
      timezone: "UTC"
    }
  ]

>>> $article->save();
=> true

#查看数据结果,添加了一条记录

>>> App\Article::all()->toArray();
=> [
    [
      "id"      => "1",
      "title"    => "My First Article",
      "body"     => "Some content...",
      "published_at" => "2015-03-28 06:37:22",
      "created_at"  => "2015-03-28 06:38:53",
      "updated_at"  => "2015-03-28 06:38:53"
    ]
  ]

>>> $article->title = 'My First Update Title';
=> "My First Update Title"

>>> $article->save();
=> true

>>> App\Article::all()->toArray();
=> [
    [
      "id"      => "1",
      "title"    => "My First Update Title",
      "body"     => "Some content...",
      "published_at" => "2015-03-28 06:37:22",
      "created_at"  => "2015-03-28 06:38:53",
      "updated_at"  => "2015-03-28 06:42:03"
    ]
  ]
  
>>> $article = App\Article::find(1);
=> <App\Article #000000005c4b7e1600000000ab91a676> {
    id: "1",
    title: "My First Update Title",
    body: "Some content...",
    published_at: "2015-03-28 06:37:22",
    created_at: "2015-03-28 06:38:53",
    updated_at: "2015-03-28 06:42:03"
  }

>>> $article = App\Article::where('body', 'Some content...')->get();
=> <Illuminate\Database\Eloquent\Collection #000000005c4b7e1800000000ab91a676> [
    <App\Article #000000005c4b7e1b00000000ab91a676> {
      id: "1",
      title: "My First Update Title",
      body: "Some content...",
      published_at: "2015-03-28 06:37:22",
      created_at: "2015-03-28 06:38:53",
      updated_at: "2015-03-28 06:42:03"
    }
  ]

>>> $article = App\Article::where('body', 'Some content...')->first();
=> <App\Article #000000005c4b7e1900000000ab91a676> {
    id: "1",
    title: "My First Update Title",
    body: "Some content...",
    published_at: "2015-03-28 06:37:22",
    created_at: "2015-03-28 06:38:53",
    updated_at: "2015-03-28 06:42:03"
  }
>>> 

>>> $article = App\Article::create(['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]);
Illuminate\Database\Eloquent\MassAssignmentException with message 'title'

MassAssignmentException,laravel保护我们不能直接插入记录。比如,在一些特殊情况下我们需要直接利用表单的信息填充数据库记录,但是如果我们并没有在表单中添加密码字段,而黑客产生了密码字段连同我们的其他字段一起送回服务器,这将产生修改密码的危险,所以我们必须明确的告诉laravel我们的模型那些字段是可以直接填充的。

修改我们的模型文件 Article.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

 protected $fillable = [
    'title',
    'body',
    'published_at'
  ];

}

表示,title, body, published_at 是可以直接填充的。

退出 tinker,重新进入

>>> $article = App\Article::create(['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]);
=> <App\Article #000000005051b2c7000000007ec432dd> {
    title: "New Article",
    body: "New body",
    published_at: <Carbon\Carbon #000000005051b2c6000000007ec4081d> {
      date: "2015-03-28 06:55:19",
      timezone_type: 3,
      timezone: "UTC"
    },
    updated_at: "2015-03-28 06:55:19",
    created_at: "2015-03-28 06:55:19",
    id: 2
  }
  
# It's ok

>>> App\Article::all()->toArray();
=> [
    [
      "id"      => "1",
      "title"    => "My First Update Title",
      "body"     => "Some content...",
      "published_at" => "2015-03-28 06:37:22",
      "created_at"  => "2015-03-28 06:38:53",
      "updated_at"  => "2015-03-28 06:42:03"
    ],
    [
      "id"      => "2",
      "title"    => "New Article",
      "body"     => "New body",
      "published_at" => "2015-03-28 06:55:19",
      "created_at"  => "2015-03-28 06:55:19",
      "updated_at"  => "2015-03-28 06:55:19"
    ]
  ]

>>> $article = App\Article::find(2);
=> <App\Article #000000005051b22b000000007ec432dd> {
    id: "2",
    title: "New Article",
    body: "New body",
    published_at: "2015-03-28 06:55:19",
    created_at: "2015-03-28 06:55:19",
    updated_at: "2015-03-28 06:55:19"
  }

>>> $article->update(['body' => 'New Updaet Body']);
=> true

#update自动调用save()

以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。

PHP 相关文章推荐
默默简单的写了一个模板引擎
Jan 02 PHP
php 调用远程url的六种方法小结
Nov 02 PHP
PHP对象转换为数组函数(递归方法)
Feb 04 PHP
解析curl提交GET,POST,Cookie的简单方法
Jun 29 PHP
使用PHP备份MySQL和网站发送到邮箱实例代码
Nov 28 PHP
php curl模拟post提交数据示例
Dec 31 PHP
destoon公司主页模板风格的添加方法
Jun 20 PHP
11个PHPer必须要了解的编程规范
Sep 22 PHP
PHP生成plist数据的方法
Jun 16 PHP
php die()与exit()的区别实例详解
Dec 03 PHP
浅谈PHP中new self()和new static()的区别
Aug 11 PHP
php+redis实现商城秒杀功能
Nov 19 PHP
Laravel 5框架学习之数据库迁移(Migrations)
Apr 08 #PHP
Laravel 5框架学习之环境与配置
Apr 08 #PHP
Laravel 5框架学习之Blade 简介
Apr 08 #PHP
Laravel 5框架学习之向视图传送数据(进阶篇)
Apr 08 #PHP
Laravel 5框架学习之向视图传送数据
Apr 08 #PHP
Laravel 5框架学习之路由、控制器和视图简介
Apr 07 #PHP
Laravel 5框架学习之Laravel入门和新建项目
Apr 07 #PHP
You might like
解决FastCGI 进程超过了配置的活动超时时限的问题
2013/07/03 PHP
PHP循环函数使用介绍之PHP基础入门教程
2013/09/21 PHP
php使用QueryList轻松采集js动态渲染页面方法
2018/09/11 PHP
PHP数组实际占用内存大小原理解析
2020/12/11 PHP
jQuery第三课 修改元素属性及内容的代码
2010/03/14 Javascript
一个网马的tips实现分析
2010/11/28 Javascript
js setTimeout opener的用法示例详解
2013/10/23 Javascript
2014最热门的JavaScript代码高亮插件推荐
2014/11/25 Javascript
基于JavaScript实现动态创建表格和增加表格行数
2015/12/20 Javascript
JavaScript中各种引用类型的常用操作方法小结
2016/05/05 Javascript
jQuery实现的背景颜色渐变动画效果示例
2017/03/24 jQuery
jQuery、layer实现弹出层的打开、关闭功能
2017/06/28 jQuery
微信小程序出现wx.navigateTo页面不跳转问题的解决方法
2017/12/26 Javascript
Vue+ElementUI使用vue-pdf实现预览功能
2019/11/26 Javascript
使用Webpack 搭建 Vue3 开发环境过程详解
2020/07/28 Javascript
JavaScript数组排序的六种常见算法总结
2020/08/18 Javascript
JavaScript中遍历的十种方法总结
2020/12/15 Javascript
js实现简单的倒计时
2021/01/28 Javascript
[04:03]DOTA2肉山黑名单梦之声 风暴之灵中文配音鉴赏
2013/07/03 DOTA
[02:50]【扭转乾坤,只此一招】DOTA2永雾林渊版本开启新篇章
2020/12/22 DOTA
打印出python 当前全局变量和入口参数的所有属性
2009/07/01 Python
Python和Ruby中each循环引用变量问题(一个隐秘BUG?)
2014/06/04 Python
Python代码的打包与发布详解
2014/07/30 Python
Phantomjs抓取渲染JS后的网页(Python代码)
2016/05/13 Python
matplotlib绘图实例演示标记路径
2018/01/23 Python
用python 批量更改图像尺寸到统一大小的方法
2018/03/31 Python
python基础教程项目二之画幅好画
2018/04/02 Python
python将回车作为输入内容的实例
2018/06/23 Python
HTML5中的强制下载属性download使用实例解析
2016/05/12 HTML / CSS
html5中canvas图表实现柱状图的示例
2017/11/13 HTML / CSS
HTML5实现直播间评论滚动效果的代码
2020/05/27 HTML / CSS
工程现场管理求职自荐信
2013/10/02 职场文书
顶岗实习计划书
2014/01/10 职场文书
工作时间上网检讨书
2014/02/03 职场文书
关于倡议书的范文
2015/04/29 职场文书
Go语言带缓冲的通道实现
2021/04/26 Golang