EloquentRelationship (one-to-many, one-to-one) prerequisite database data
Users table id name email password remember_token created_at updated_at1 123456 123@qq.com $ 2y $10 $ EfEo1gCcK6JdwgxjqjNbK. fVZjgu7i68uKMPqNwBX9VpNVuvgthm6 15:31:07: 53articles table id title content publish_at created_at updated_at your title content 18:04:44 18:04:48 1 minute id title content publish_at created_at updated_at your title2 content2 18:04:48 04:24:48 1
User_id is used as the default foreign key name. because the primary key in the user table is id, the eloquent module of laravel automatically processes this, and maintains a foreign key association in itself.
One-to-one relationship
For example, a User model has a corresponding article model, and a User has an article
App/User. php public function articles-one () {return $ this-> hasOne ('app \ Articles '); // The hasOne method is provided by laravel, directly use the // input parameter in the model as another model, which is abstract and parsed as a one-to-one association between a table and another table}
Use the command line tool tinker to demonstrate
Php artisan tinkerPsy Shell v0.7.2 (PHP 5.5.34-cli) by Justin Hileman >>>> user = App \ User: first () ;=> App \ User {# 691 id: 1, // This is the user's primary key, in fact, is the users table's primary key name: "123456", email: "123@qq.com", created_at: "15:31:07", updated_at: "15:41:53", >>>>$ user-> articles-one; // call the articles-one method just written. the purpose of writing this method is here, when instantiating this model, add a method that can be operated and implement the design we need. => App \ Articles {#692 id: 1, title: "title", content: "content", publish_at: "18:04:44", created_at: "18:04:48", updated_at: "18:04:48", user_id: 1, // The id key of the user model is automatically associated by default, which is the default setting of laravel, after Association, the foreign key of model name + primary key is automatically generated. // This indicates that this article belongs to the user with id 1. >>>>
If you do not want to use the default foreign key name, return $ this-> hasOne ('app \ Articles ', 'foreign _ key', 'local _ key '); input a foreign_key parameter, and then add your custom key.
One-to-many relationship
A User model has a corresponding article Model. a User has multiple articles.
App/User. php public function articles () {// change articles-one to articles to indicate many articles, not just a return $ this-> hasales ('app \ Articles ');}
Use the command line tool tinker to demonstrate
Php artisan tinkerPsy Shell v0.7.2 (PHP 5.5.34-cli) by Justin Hileman >>>> user = App \ User: find (1) ;=> App \ User {# 693 id: 1, name: "123456", email: "123161769@qq.com", created_at: "15:31:07", updated_at: "15:41:53", >>>> $ articles = App \ User :: find (1)-> articles; // The find method is provided by laravel by default. the query method is used and the id parameter is passed in, so here it means to find all the data whose id is 1 // and then use the chained method to continue filtering the results based on the original and execute the articles method, after the combination, all articles that belong to user id 1 will be returned. // In fact, it is equivalent to writing select * from articles where user_id = 1. (of course, there are multiple table queries, this is not an executable SQL syntax. here we only want to facilitate the conversion of thinking) => Illuminate \ Database \ Eloquent \ Collection {#702 all: [App \ Articles {#703 id: 1, title: "title", content: "content", publish_at: "18:04:44", created_at: "18:04:48", updated_at: "18:04:48", user_id: 1 ,}, app \ Articles {#704 id: 2, title: "title2", content: "content2", publish_at: "04:24:48", created_at: "18:07:42", updated_at: "18:07:42", user_id: 1 ,},
One-to-many instances have a reverse lookup usage and define a relative Association.
For example, if a user has multiple articles in a one-to-many relationship, you can set this article to find the user.
Since it is reverse lookup, it is done in the model of the article.
App/Articles. php public function user () {return $ this-> belonsto ('app \ user'); // this keyword is the same as the meaning, the article contains the User model. the returned user // mechanism is still completed through the foreign key, and is associated with the user id through the foreign key user_id of articles}
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", }>>>