Laravel 5 Framework Learning eloquent (Laravel orm), laraveleloquent
Let's build the first model.
Copy the Code code as follows:
PHP Artisan Make:model Article
#输出
Model created successfully.
Created migration:2015_03_28_062517_create_articles_table
Look at the resulting file app/article.php
<?php namespace App;use Illuminate\database\eloquent\model;class article extends Model {//}
Nothing special, except that it inherits from the model, but has powerful functions, which are encapsulated in the model of Laravel. The model automatically has powerful features such as Save () update () findxxx ().
Tinker is a command-line tool provided by Laravel and can interact with the project.
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 F Irst 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" = "Some 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 "=" 2015-03-28 06:38:53 "," Updated_at "and" 2015-03-28 06:38: "]]>>> $article->title = ' My first update title ';=> ' My first update title ' >>> $article-&G T;save ();=> true>>> App\article::all ()->toarray ();=> [["id" = "1", "title" =& Gt "My first Update Title", "Body" and "Some content ...", "published_at" = "2015-03-28 06:37:22", "C Reated_at "=" 2015-03-28 06:38:53 "," Updated_at "and" 2015-03-28 06:42:03 "]" >>> $article = A Pp\article::find (1);=> {ID: "1", Title: "MY first Update Title ", Body:" Some content ... ", Published_at:" 2015-03-28 06:37:22 ", Created_at:" 2015-03-28 06:3 8:53 ", Updated_at:" 2015-03-28 06:42:03 "}>>> $article = app\article::where (' body ', ' Some content ... ')->ge T ();=>
[{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: Updated_at: "2015-03-28 06:42:03"}]>>> $article = App\article::where (' body ', ' Some content ... ')-& Gt;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 "}>>> >>> $artic Le = app\article::create ([' title ' = ' new article ', ' body ' = ' new body ', ' published_at ' and ' Carbon\carbon::now ()]
); illuminate\database\eloquent\massassignmentexception with message ' title '
Massassignmentexception,laravel Protection We cannot insert records directly. For example, in some special cases we need to populate database records directly from the form's information, but if we do not add a password field to the form, and the hacker generates a password field to be sent back to the server along with our other fields, this will create the risk of changing the password. So we have to explicitly tell Laravel that our model fields are directly populated.
Modify our model file article.php
<?php namespace App;use Illuminate\database\eloquent\model;class article extends Model {protected $fillable = [ ' Title ', ' body ', ' published_at ' ];}
That is, the title, body, Published_at can be directly filled.
Exit Tinker, re-enter
>>> $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:1 9 ", 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" and "Some content ...", "p Ublished_at "=" 2015-03-28 06:37:22 "," created_at "=" 2015-03-28 06:38:53 "," Updated_at "and" 2015-0 3-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);=> {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- 06:55:19 "}>>&Gt $article->update ([' body ' = ' New updaet body ']);=> true#update automatically call Save ()
The above mentioned is the whole content of this article, I hope to be able to learn LARAVEL5 framework to help you.
http://www.bkjia.com/PHPjc/980213.html www.bkjia.com true http://www.bkjia.com/PHPjc/980213.html techarticle Laravel 5 Framework Learning eloquent (Laravel ORM), laraveleloquent us to generate the first model copy code code as follows: PHP artisan Make:model article #输出 model C Reat ...