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