V
V
Volodymyr2022-04-15 12:26:42
Database design
Volodymyr, 2022-04-15 12:26:42

How to properly write and store a list of data in Laravel?

There is a users table and there is a User model . In the personal account, each user has two lists of data that he can store and expand, this is a list of his phones and short notes.
Hence the question of how to correctly write and store a list of data in Laravel?

You can create two columns in the table called phones and notes, and store all lists in them separated by commas, for example, for each user and take them out through explode. But as I understand it is not quite right? Well, the issue of speed and resource consumption is also important.
Who can tell what is right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2022-04-15
@volodyalesha86

Depends on how it's used. In any case, it is not advisable to store in the same table with the user, it is too much data to read with each request. The choice remains between one-to-one (user id as a primary key or a unique index and a json data field) and one-to-many (a separate primary key and a non-unique foreign index with a user id, each line has one record separately) .
In this case, the one-to-many option would be more logical (and more convenient for adding / editing, etc.), but in some cases (not this use case, of course), one-to-one may also be suitable.
I advise you to read this section in the documentation in full: https://laravel.com/docs/8.x/eloquent-relationships

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question