lirone
1 week ago

Get user's record via model ?

Posted 1 week ago by lirone

I'm starting learning Laravel and I wonder about the way to retrieve some data. I have 4 tables

  • Users
  • Records
  • RecordsA
  • RecordsB

An Users may have multiple records. (Records has a foreign key of user) RecordsA has a foreign key of Records RecordsB has a foreign key of Records

I would like to retrieve records (A or B) from an User and I wonder what is the best way to do it.

  1. Write functions to the user's repository getRecordA() and getRecordB() so that I could do in my controller, $this->user->getRecordA() and user->getRecordB()

  2. Write functions to recordA's repository and recordB's repository ? So I will inject the id of the user in the query. So in my controller, I could write recordA->get(User $user)

I'm asking that, because the user is the core of my app and I have many tables (among Records, RecordsA and RecordB) that are linked to my user and I'm affraid that my user table will be huge. However, for me it's intuitive to put all the related function to the user repository.

What do you think ? Could you please give the good practice ?

Please sign in or create an account to participate in this conversation.