Eloquent (laravel's ORM) and laraveleloquent_PHP tutorial for Laravel5 framework learning

Source: Internet
Author: User
Laravel5 framework learning: Eloquent (laravel's ORM) and laraveleloquent. Eloquent (laravel ORM) for Laravel5 framework learning. the code for copying the first model is as follows: phpartisanmake: modelArticle # output Modelcreat Laravel 5 framework learning Eloquent (laravel ORM), laraveleloquent

Let's generate the first model.

The code is as follows:
Php artisan make: model Article
# Output
Model created successfully.
Created Migration: 2015_03_28_062517_create_articles_table

Check the generated file app/Article. php.

<?php namespace App;use Illuminate\Database\Eloquent\Model;class Article extends Model { //}

Nothing special, except for inheriting from the Model, but with powerful functions, these are encapsulated in the laravel Model. The model automatically has powerful functions such as save () update () findXXX.

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 file Article. php

<?php namespace App;use Illuminate\Database\Eloquent\Model;class Article extends Model { protected $fillable = [    'title',    'body',    'published_at'  ];}

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 is all the content of this article, hoping to help you learn the Laravel5 framework.

Http://www.bkjia.com/PHPjc/980213.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/980213.htmlTechArticleLaravel 5 framework learning Eloquent (laravel's ORM), laraveleloquent we will generate the first model code as follows: php artisan make: Model Article # output model creat...

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.