The relationship between Laravel5 learning tables

Source: Internet
Author: User

A user can have many article, and a article corresponds to a user.

In the migration of article

 Publicfunction up () {schema::create ('Articles', function (Blueprint $table) {$table->increments ('ID'); $table->integer ('user_id'),unsigned (); $table-string('title'); $table-string('Body'); $table->timestamp ('Publish_at'); $table-timestamps (); $table->foreign ('user_id')                  ->references ('ID')                  ->on ('Users')                  ->ondelete ('Cascade');    }); }

In the model of article

  // An article belongs to an author     Public function User () {        return $this->belongsto ('app\user ' );    }

In the user's model

// a user has a lot of articles     Public function articles () {        return $this->hasmany ('app\articles ' );    }

If the information of the article is $article, then using $article->user, you can get the corresponding user's information.

Similarly, if the query out the user's information is $user, with $user->article, you can get the information of this user corresponding article.

The relationship between Laravel5 learning tables

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.