Eloquent ORM Learning Notes

Source: Internet
Author: User
Tags php tutorial
   recently in learning Laravel, I think the ORM function is very powerful, I'm just a little explored here, if there are better notes, please share.

Because the focus is eloquent ORM, so the routing settings, the controller is not described in detail, here directly into the models module.

1. Database Preparation (MYSQL)

Here you need to create two tables of users and Users_ext, and initialize the data:  

1 CREATE TABLE2 IF  not EXISTSUsers (3IautoidINT( One) not NULLAuto_increment,4SnmameVARCHAR( -),5IstatusTINYINT(4),6IcreatetimeINT( One),7     PRIMARY KEY(iautoid)8) ENGINE=INNODBDEFAULTCHARSET=UTF8 auto_increment= 1;9 Ten INSERT  intousers (Snmame, Istatus, Icreatetime) One VALUES A('test1',1,1400085387), -('test2',1,1400085387), -('test3',1,1400085387), the('test4',1,1400085387), -('Test5',1,1400085387), -('Test6',1,1400085387); -  + CREATE TABLE - IF  not EXISTSUsers_ext ( +IautoidINT( One) not NULLAuto_increment, AIageDECIMAL(3,0), atSSexTINYINT(4), -IuseridINT( One), -     PRIMARY KEY(iautoid) -) ENGINE=INNODBDEFAULTCHARSET=UTF8 auto_increment= 1; -  - INSERT  intoUsers_ext (iage, SSex, Iuserid) in VALUES -( -,1,1), to(109,0,2), +( -,1,3), -( -,1,5), the( the,0,4), *( -,1,6);

 2. Configure the database

To add a database configuration in database.php:

  

 
 1 
   PHP 2  3return  [  4  5     ' fetch ' = Pdo::fetch_class, 6
    7     ' default ' = ' user ', 8  9     ' connections ' = [10          ' user ' = [One ' driver ' + '             mysql ', '                 Host '      = ' localhost:3306 ','             database '  = ' test ',             ' username '  = ' root ',' password ' + ' root ', '               charset ' = '   utf8 ',  collation ' + '             utf8_unicode_ci ', ' prefix' and '    ,         ',  -     ];

  3. Create Usermodel

Create a usermodel.php under App\models  

 
 1 
   PHP23classextends  \eloquent {4     protected $table = ' users '; 5     protected $primaryKey = ' iautoid '; 6     protected $connection = ' user '; 7 }

  Such a user model is created successfully and the code is simple. As for the meaning of the member variables, I believe they can be understood, which in turn represents the table name, primary key, database connection identity (in the configuration file).

5. Using Usermodel

Now you can use them anywhere. Controller, the route can be used inside. Here's a simple sample:

Now there are query statements:  

1 SELECT 2     users.snmame,  3    users.icreatetime , 4    users_ext.iage, 5     users_ext.ssex  6 from 7    users   8  leftJOINon=  users_ext.iuserid  9WHERE Ten     = 1  One  and = 0  A ORDER  by  -     users.icreatetime0 ,  1

 Here is a simple query statement, which is then implemented in the form of an ORM:

1  Public function getusers () {23         $select = ' users.snmame,users.icreatetime,users_ext.iage,users_ext.ssex '; 4         $resData = Usermodel::selectraw ($select)->leftjoin (' Users_ext ', ' users.iautoid ', ' = ', ' Users_ext.iuserid ')->where (' users.istatus ', ' = ', 1)->where (' users_ext.ssex ', ' = ', 0)->skip (0)->limit (1 ),get (); 5         Var_dump ($resData,ToArray ()); 6         Exit (); 7 }

  Here are the results of the query:

  

 OK, the above is just a simple example of the query, there are many need to study, such as the correlation between modules and so on.

  

  

  

The above describes the eloquent ORM learning notes, including aspects of the content, I hope that the PHP tutorial interested in a friend 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.