Laravel5 basics (7)-Eloquent (laravel's ORM)

Source: Internet
Author: User
: This article mainly introduces Laravel5 basics (7)-Eloquent (laravel's ORM). If you are interested in PHP tutorials, refer to it.
  • Let's generate the first model.
Php artisan make: model Article # output Model created successfully. Created Migration: 2015_03_28_062517_create_articles_table

View the generated fileapp/Article.php


  

Nothing special, except for inheriting from the Model, but with powerful functions, these are encapsulated in the laravel Model. The model automatically hassave() update() findXXX()And other powerful functions.

  • Tinker is a command line tool provided by laravel that can interact with projects.
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 Jinglin '; => "zhang jinglin" >>>$ name => "zhang jinglin" >>>$ article = new App \ Article; >{}>>>$ article-> title = 'My First article '; => "My First Article" >>>$ article-> body = 'some content... '; => "Some content... ">>>$ article-> published_at = Carbon \ Carbon: now (); =>  {Date: "06:37:22", timezone_type: 3, timezone: "UTC" >>>$ article ;=>{ title: "My First Article", body: "Some content... ", published_at:  {Date: "06:37:22", timezone_type: 3, timezone: "UTC" }>>>$ article-> toArray (); => ["title" => "My First Article", "body" => "Some content... "," published_at "=>  {Date: "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" => "Some content... "," published_at "=>" 06:37:22 "," created_at "=>" 06:38:53 ", "updated_at" => "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 "=>" 06:37:22 "," created_at "=>" 06:38:53 ", "updated_at" => "06:42:03"]> $ article = App \ Article: find (1) ;=>{ id: "1", title: "My First Update Title", body: "Some content... ", published_at:" 06:37:22 ", created_at:" 06:38:53 ", updated_at:" 06:42:03 ">>>> $ article = App \ Article: where ('body ', 'Some content... ')-> get (); =>  [{Id: "1", title: "My First Update Title", body: "Some content... ", published_at:" 06:37:22 ", created_at:" 06:38:53 ", updated_at:" 06:42:03 "}] >>> $ article = App \ Article :: where ('body', 'Some content... ')-> first () ;=>{ id: "1", title: "My First Update Title", body: "Some content... ", published_at:" 06:37:22 ", created_at:" 06:38:53 ", updated_at:" 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 and laravel protect that records cannot be inserted directly. For example, in some special cases, we need to directly use the form information to fill the database records, but if we do not add a password field to the form, hackers generate password fields and other fields to send them back to the server, which may cause the risk of changing the password, therefore, we must clearly tell laravel that the fields of our model can be directly filled.

Modify our model fileArticle.php

 

Title, body, and published_at can be directly filled.

Exit tinker and enter again

>>>$ Article = App \ Article: create (['title' => 'new article', 'body' => 'new body ', 'hhed _ at' => Carbon \ Carbon: now ()]); =>{ title: "New Article", body: "New body", published_at:  {Date: "06:55:19", timezone_type: 3, timezone: "UTC"}, updated_at: "06:55:19", created_at: "06:55:19", id: 2} # It's OK >>> App \ Article: all ()-> toArray () ;=> [["id" => "1 ", "title" => "My First Update Title", "body" => "Some content... "," published_at "=>" 06:37:22 "," created_at "=>" 06:38:53 "," updated_at "=>" 06:42:03 "], ["id" => "2", "title" => "New Article", "body" => "New body", "published_at" => "06:55:19 ", "created_at" => "06:55:19", "updated_at" => "06:55:19"] >>>> $ article = App \ Article: find (2 ); ==>{ id: "2", title: "New Article", body: "New body", published_at: "06:55:19", created_at: "06:55:19", updated_at: "06:55:19" >>>> $ article-> update (['body' => 'New Updaet body']); => true # update automatically calls save () 

The above introduces Laravel 5 basics (7)-Eloquent (laravel's ORM), including some content, and hopes to help friends who are interested in PHP tutorials.

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.