Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nagraj722's avatar

Laravel relationship suggestion

Hi.. i need a suggestion, i have a two tables articles and authors. my scenario is every article have more then one author. My problem is, how to get author data with article. i know the solution with belongsToMany relationship with pivot table. is there any solution without use pivot table. Thanks..

Article_table 
---------------------------------------
| id | title      | author_id   |
---------------------------------------
| 1  | title 1   | 1,2               |
---------------------------------------
| 2  | title 2   | 3,4,5            | 
---------------------------------------

Authors_table 
-----------------------
| id | name      |
-----------------------
| 1  | name 1   |
-----------------------
| 2  | name 2   |
-----------------------
| 3  | name 3   |
-----------------------
| 4  | name 4   |
-----------------------
0 likes
3 replies
Cronix's avatar

The pivot table is the correct solution. You shouldn't have a column containing multiple ids. You can't index that and it will get slower and slower as the tables grow. You also can't use relationships or joins on them without a bunch of hacky workarounds because that's not how a relational database works.

Snapey's avatar

its about 10 minutes work to add a pivot, followed by hours and hours of being thankful that you did

Think of it the other way. Suppose you needed to find out what books author 3 had written. How would you do that doing it your way?

Please or to participate in this conversation.