@
@
@Twitt2019-10-06 14:21:05
Laravel
@Twitt, 2019-10-06 14:21:05

Are models needed for adjacent tables?

Let's say there is a users table, there is a bank_account, and one user can have several bank accounts - the users_bank_account table.
If I created a model for the users table and bank_account, do I need a model for the pivot table - users_bank_account? If necessary, then why? All requests which concerning this table will lay down in this model simply?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2019-10-06
_

For example, for some reason, you needed to display all the pivot records from user_roles on the page and display the user's name instead of user_id. You create a UserRole model, write belongsTo User, make a UserRole with user request and show $pivot->user->FullName in the template, do some kind of search with sorting and filtering by pivot records, make links to the user, display the role name, statistics - all this visually with simple code and only the necessary queries. In general, do it when there is a need in the pivot model, as in an independent logical entity, when you start from it and not come to it, when you are not interested in users / roles, but pivot data with its relationships / methods.

J
JhaoDa, 2019-10-06
@JhaoDa

do i need a model for pivot table
If necessary, then why?

Sometimes you need it, most of the time it doesn't. This question can only be answered by knowing the problem.
All requests which concerning this table will lay down in this model simply?
Ehehe, “requests will lie down” ... well, yes, they will.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question