Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of one-to-many relationship in laravel orm

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of one-to-many relationship in laravel orm, which is very detailed and has certain reference value. Friends who are interested must finish it!

For the understanding of the one-to-many relationship in laravel orm

Article table article, the article can be commented naturally, table comment records the comments of the article, the relationship between the article and comments is one-to-many, an article can have more than one comment. In the comment table, there is a field article to record the article to which the comment belongs. The relationship between the article and the comment is as follows:

Article:id......

Comment: id...... Article_id

There is a foreign key article_id associated with article in the comment table, so it is the belongsTo method in the Comment model and the hasMany method in the Article model

In the model Article of the article, you can associate comments in the following ways

Public function comments () {

Return $this- > hasManay ('Comment (here is the model to be associated, this example is the comment model Comment),' article_id' (here is the field name of the associated foreign key, and this example is the article_id field), 'id' (corresponding to the primary key of the associated model, where id is the id of the associated article table))

}

Correspondingly, in the Comment model, you can associate articles in the following ways

Public function article () {

Return $this- > belongsTo ('Article (here is the model to be associated, this example is the article model Article),' article_id' (here is the field name of the associated foreign key, and this example is the article_id field), 'id' (corresponding to the primary key of the associated model, where id is the id of the associated article table))

}

The above is all the contents of the article "example Analysis of one-to-many relationships in laravel orm". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report