P
P
photosho2016-01-06 14:07:59
Laravel
photosho, 2016-01-06 14:07:59

What actions should be contained in the models?

Hello. The question is about understanding the model in MVC, and more specifically in Laravel. Let's say we have a user. We need to:
1. Ensure its registration. To do this, you need, at a minimum, to compare the "username" of the user with all the records available in the "users" table - to check whether a user with this name has already met or not. How it's done? In the "user" model class, the "getUsersCount(field)" method is created, where the name of the field by which we will search is passed and which, by making an appropriate query to the database, returns the number of records found by this field. Correct understanding?
2. Provide data change in the user profile. A method is created in the model to save a field or a group of fields to the database, and by calling this method, the controller saves specific fields for a specific user (for example, we edit using Ajax). Is this correct?
3. Ensure user deletion. Some method that causes a particular user's row to be deleted from the database. Not everything is clear here. If the user managed to write comments or messages on the forum, they also, it seems, should be deleted. How is this done? Should the controller first access the "user" model, deleting a specific user, and then - the comment, post, article models - to delete records corresponding to the deleted user?
I just started to understand all this, and my understanding of the structure of models in the MVC system may not be correct yet. I understand it this way: a model is not an object, but some interface for interacting with a table in a database. You can get the value of a specific field from the database by querying a specific method of this class, passing the "id" of the required string and column name. I just feel that this understanding is not entirely correct.
And the last question for this post. Let's say we have a table of articles and a table of comments. Each comment has an "article_id" column, the id of the article it belongs to. There is also a page with a list of all articles, and a page displaying the full version of the article. The list page should display not only information about the article, but also the number of comments to it (not to mention the author's name, also obtained by its identifier).
On the page of the full version of the article, the comments to it should also be displayed. We see that here the task goes far beyond the framework of one model. How is their interaction ensured in this case? Is the task of collecting a common object / array with data to pass to the display - related to the controller or should be performed at the model level?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-01-06
@Denormalization

Points 1, 2 and 3 are already in Laravel (Eloquent) out of the box.
Everything else is there too.
Read the documentation , watch the video . All these questions have been answered for a long time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question