Laravel 5 Basics (vii)-eloquent (Laravel's ORM)

Source: Internet
Author: User
    • Let's build the first model.
php artisan make:model Article#输出Model created successfully.Created Migration: 2015_03_28_062517_create_articles_table

Look at the resulting fileapp/Article.php


  
   

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

    • tinker 是 laravel提供的命令行工具,可以和项目进行交互。
PHP Artisan tinker# The following is the interactive input in Tinker psy Shell v0.4.1 (php 5.4.16-cli) by Justin hileman>>> $name = ' Zhang Jingli N ';=> "Zhang Jinglin" >>> $name = "Zhang Jinglin" >>> $article = new App\article;=> {}>>& Gt $article->title = ' My first article ';=> "my first article" >>> $article->body = ' Some content ... ';=> ' Some content ... ">>> $article->published_at = Carbon\carbon::now ();=>  {date: "2015-03-28 06:37:22", Timezone_type:3, TimeZone: "UTC"}>>> $article;=> { Title: "My First article", Body: "Some content ...", Published_at:  {date: "2015-03-28 06:37:22", Timezone_type:3, TimeZone: "UTC"}} >>> $article->toarray ();=> ["title" + "My first article", "body" = "Som" E content ... "," published_at "=  {date: "2015-03-28 06:37:22", Timezone_type:3, TimeZone: "UTC"}] >>> $article->save ();=> true# View data results, added a record >>> app\article::all ()->toarray ();=> [[ "id" = "1", "title" = "My First article", "Body" and "Some" Content ... "," published_at "=" 2015-03-28 06:37:22 "," Created_at "and" 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", "Bod Y "=" Some content ... "," published_at "=" 2015-03-28 06:37:22 "," created_at "and" = " 2015-03-28 06:38:53 "," Updated_at"= =" 2015-03-28 06:42:03 "] >>> $article = app\article::find (1);=> {ID:" 1 ", t Itle: "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 ', ' Som E content ... ')->get ();=>   [{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 C Ontent ')->first ();=> {id: "1", Title: "My first Update title", Body: "Some content ...", Published_at: "2015-03-2 8 06:37:22 ", Created_at:" 2015-03-28 06:38:53 ", Updated_at:" 2015-03-28 06:42:03 "}>>> >>> $article = Ap P\article::create ([' title ' = ' new article ', ' body ' = ' new body ', ' published_at ' = Carbon\carbon::now ()]); Lluminate\database\eloquent\massassignmentexception with message ' title '     

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

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

 

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

退出 tinker,重新进入

>>> $article = app\article::create ([' title ' = ' = ' new article ', ' body ' = ' new body ', ' published_at ' = Ca Rbon\carbon::now ()]);=> {title: "new article", Body: "New Body", Published_at:  {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 "," Crea Ted_at "=" 2015-03-28 06:38:53 "," updated_at "=" 2015-03-28 06:42:03 "], [" ID "+" 2 "," title "=" New article "," Body "and" 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);=> {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 automatically call Save () 

Above introduces the Laravel 5 Foundation (vii)-eloquent (Laravel ORM), including the aspects of the content, want to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.