Eloquent ORM Learning Notes

Source: Internet
Author: User

  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<?PHP2 3 return [4 5' Fetch ' = Pdo::fetch_class,6 7' Default ' = ' user ',8 9' Connections ' = [Ten' User ' = [ One' Driver ' = ' mysql ', A' Host ' = ' localhost:3306 ', -' Database ' = ' test ', -' Username ' = ' root ', the' Password ' = ' root ', -' CharSet ' = ' utf8 ', -' Collation ' = ' utf8_unicode_ci ', -' Prefix ' = ', +], -     ] +];

  3. Create Usermodel

Create a usermodel.php under App\models  

 1  <? php  2   Class  Usermodel extends   \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 SELECT2 Users.snmame,3 Users.icreatetime,4 Users_ext.iage,5 Users_ext.ssex6  from7 Users8  Left JOINUsers_ext onUsers.iautoid=Users_ext.iuserid9 WHERETenUsers.istatus= 1 One  andUsers_ext.ssex= 0 A ORDER  by - Users.icreatetime -LIMIT0, the  1

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

1  Public functiongetusers () {2 3         $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.

  

  

  

Eloquent ORM Learning Notes

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.