Let's build the first model.
Copy Code code as follows:
PHP Artisan Make:model Article
#输出
Model created successfully.
Created migration:2015_03_28_062517_create_articles_table
View the generated Files app/article.php
<?php namespace App;
Use Illuminate\database\eloquent\model;
Class Article extends Model {
//
}
Nothing special, in addition to inheriting from the model, but with powerful features, these are encapsulated in the Laravel model. The model automatically has powerful features such as Save () update () findxxx ().
Tinker is a command-line tool provided by Laravel that can interact with projects.
PHP artisan Tinker #以下是在tinker中的交互输入 Psy Shell v0.4.1 (php 5.4.16-cli) by Justin hileman >>> $name = ' Zhang J
Inglin ';
=> "Zhang Jinglin" >>> $name => "Zhang Jinglin" >>> $article = new App\article;
=> <app\article #000000005c4b7ee400000000ab91a676 > {} >>> $article->title = ' My A-Article ';
=> "My 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 I-Article", Body: "Some content ...", Published_at: <carbon\carbon #000000005c4b7ee600000000ab91dcb6 > {date: "2015-03-28 06:37:22", Timez One_type:3, TimeZone: "UTC"}} >>> $article->toarray (); => ["title" => "My-Article", "Body" => "Some content ...", "Published_at" => <carb On\carbon #000000005c4b7ee600000000ab91dcb6 > {date: "2015-03-28 06:37:22", Timezone_type:3, Timezon
E: "UTC"}] >>> $article->save ();
=> true #查看数据结果, a record was added >>> App\article::all ()->toarray (); => [["id" => "1", "title" => "My I-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-i-Update title ';
=> "My I-Update Title" >>> $article->save ();
=> true >>> app\article::all ()->toarray (); => [["id" => "1", "title" => "My I-Update title", "Body" => "Some cont Ent ... "," PublisHed_at "=>" 2015-03-28 06:37:22 ", created_at" => "2015-03-28 06:38:53", "Updated_at" => "2015-03-2
8 06:42:03 "]] >>> $article = app\article::find (1); => <app\article #000000005c4b7e1600000000ab91a676 > {id: "1", Title: "My-i-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 I-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:0
3 "}] >>> $article = app\article::where (' body ', ' Some content ... ')->first (); => <aPp\article #000000005c4b7e1900000000ab91a676 > {id: "1", title: "My I-Update title", Body: "Some Conten T ... ", Published_at:" 2015-03-28 06:37:22 ", Created_at:" 2015-03-28 06:38:53 ", Updated_at:" 2015-03-28 06:42:0 3 "} >>> >>> $article = app\article::create ([' title ' => ' new article ', ' Body ' => ' new body ', '
Published_at ' => 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 the database records directly with the information of the form, but if we don't add the password field to the form, and the hacker generates the password field to return 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 the fields of our model can be filled directly.
Modify our model Files article.php
<?php namespace App;
Use Illuminate\database\eloquent\model;
Class Article extends Model {
protected $fillable = [
' title ',
' body ', '
published_at '
];
}
said, title, body, Published_at can be directly populated.
Quit Tinker, re-enter
>>> $article = app\article::create ([' title ' => ' new article ', ' Body ' => ' new body ', ' published_at ' => C
Arbon\carbon::now ()]); => <app\article #000000005051b2c7000000007ec432dd > {title: "New Article", Body: "New body", Publishe
D_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 I-Update title", "Body" => "Some cont Ent ... "," 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 "," bod Y "=>" 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 "]] &
gt;>> $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 ')];
=> true #update自动调用save ()
The above is the full content of this article, I hope to be able to learn LARAVEL5 framework to help.