Eloquent relationship (one-to-many, a-pair)

Source: Internet
Author: User
Data from the premise database

Users table ID  name    email   password    remember_token  created_at  updated_at1   123456  123@ qq.com  $2Y$10$EFEO1GCCK6JDWGXJQJNBK.FVZJGU7I68UKMPQNWBX9VPNVUVGTHM6    Wsar4v256k4xxisbbivns1o9ieqwcaidzb5nk5yzyj5jnbeniiowtalrbnnt    2016-05-25 15:31:07 2016-05-25 15:41:53articles table ID  title   content publish_at  created_at  updated_at  user_id1   title   content 2016-05-14 18:04:44 2016-05-14 18:04:48 2016-05-14 18:04:48 1--------------------------------ID  Title   content publish_at  created_at  updated_at  user_id2   title2  content2    2016-05-15 04:24:48 2016-05-14 18:07:42 2016-05-14 18:07:42 1

USER_ID as the default foreign key name, because the primary key inside the user table is the Id,laravel eloquent module that handles these automatically, and he maintains a foreign key correlation in itself.

A one-to-one relationship

For example, a user model has a corresponding article model, one of which has an article

app/user.php Public    function Articles-one () {        return $this->hasone (' app\articles ');//HasOne method, This method is provided by Laravel, directly in the model using                                              //Incoming parameters is another model, here is abstract, parse out is actually a table and another table of one-to-one association    }

Tinker Demo with command line tools

PHP artisan tinkerpsy Shell v0.7.2 (php 5.5.34-cli) by Justin hileman>>> $user = App\user::first ();=> App\us er {#691     id:1,   //This is the user's primary key, is actually the users table's primary key     name: "123456",     email: "123@qq.com",     created_at: " 2016-05-25 15:31:07 ",     updated_at:" 2016-05-25 15:41:53 ",   }>>> $user->articles-one;  Call the Articles-one method you just wrote, and write this method in order to add an actionable method to the model, and then implement the design that we need. = = App\articles {#692     id:1,     title: "title",     Content: "Content",     publish_at: "2016-05-14 18:04:44 ",     created_at:" 2016-05-14 18:04:48 ",     updated_at:" 2016-05-14 18:04:48 ",     user_id:1,   // Automatic Default association is the user model ID key, which is the default setting of Laravel, the association will automatically generate model name + PRIMARY KEY FOREIGN key                   //This means that this article belongs to the user ID 1.                 

If you do not want to default, you want to customize the foreign key name, return $this->hasone (' app\articles ', ' foreign_key ', ' Local_key '), pass in a foreign_key parameter, Then add one of your custom keys back.

A one-to-many relationship

A user model has a corresponding article model with multiple articles

app/user.php Public    function articles () {//For convenience of distinction, change articles-one to articles, representing many articles, not just a        return $this Hasmany (' app\articles ');    }

Tinker Demo with command line tools

PHP artisan tinkerpsy Shell v0.7.2 (php 5.5.34-cli) by Justin hileman>>> $user = app\user::find (1);=> App\us Er {#693 id:1, Name: "123456", Email: "123161769@qq.com", Created_at: "2016-05-25 15:31:07", Updated_ At: "2016-05-25 15:41:53",}>>> $articles = App\user::find (1)->articles; The Find method is provided by default, the Query method is used to know that the ID parameter is passed in, so the meaning is to find all the data with ID 1//And then laravel the original On the basis of filtering results, executing the articles method, the combination will return all the articles belonging to the user ID 1//actually equivalent to write select * from articles WH       ere user_id = 1; (Of course, there are many table queries, this is not an executable SQL syntax, here is just for the convenience of conversion thinking) = Illuminate\database\eloquent\collection {#702 all: [ app\articles {#703 id:1, title: "title", Content: "Content", Publish_at: "2016-05-14 1       8:04:44 ", Created_at:" 2016-05-14 18:04:48 ", Updated_at:" 2016-05-14 18:04:48 ", User_id:1, }, App\articles {#704 id:2, title: "Title2 ", Content:" Content2 ", Publish_at:" 2016-05-15 04:24:48 ", Created_at:" 2016-05-14 18:07:42 " , Updated_at: "2016-05-14 18:07:42", User_id:1,},

One-to-many also has a use of reverse lookup, which defines a relative correlation

For example a user with multiple articles is a one-to-many relationship, and can then be set through this article in reverse lookup to this owning user

Since it is a reverse lookup, it is in the article this model to do the processing

app/articles.php Public    function User ()    {        return $this->belongsto (' App\user ');  Belongsto is the key word, also with the meaning, is the meaning, the article belongs to the user model of the article, return user                                              //their mechanism is still through the foreign key to complete, through the articles foreign key user_id and The user's ID is associated    }
PHP artisan tinkerpsy Shell v0.7.2 (php 5.5.34-cli) by Justin hileman>>> $user = app\articles::find (1)->user ;=> app\user {#696     id:1,     name: "123456",     email: "123161769@qq.com",     created_at: "2016-05-25 15:31:07 ",     updated_at:" 2016-05-25 15:41:53 ",   
  • 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.