D
D
Dmitry Skogorev2018-06-19 20:31:35
Laravel
Dmitry Skogorev, 2018-06-19 20:31:35

What to put in the controller and what in the model?

Good afternoon everyone, I'm trying to understand OOP and using Laravel as an MVC framework and I can't figure out how to separate functions - what is done in the controller and what is in the model. That is why I have a number of questions.
For example, I have a class with attributes

id
name
age

In a db at me respectively this data is stored.
the first question is how to fill object attributes with these properties?
I see a clumsy way
$model = new MyModel();
$data = $model->findOrFail($id);
$this->name = $data->name;
$this->age = $data->age;

return $this;

Construct an object and fill attributes in a loop?
I hear that something is wrong.
Further, if I have, for example, a method that adds a unit to $age, should it be located in the controller? And if I immediately receive an array from the model and want to execute this method for each?
Thanks in advance for the clarification and I apologize for the confusion.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Romashkan, 2018-06-20
@EvgeniiR

1. Take this course: https://laracasts.com/series/laravel-from-scratch-2017 - free
2. Read the documentation for things you don't understand. There for a maximum of a week if you sit in the evenings. I think an understanding of what to put where to appear.
3. If you like the course, then there is a very high-quality and voluminous one - Lets build a forum with laravel and TDD (I'm checking it myself now, creating everything in parallel on my own), or a lot of interesting ones in php / laravel, incl. and OOP. (In general, they are inexpensive, but there are also on the rutracker / courtehunters).
Of course, I myself am not a guru, but from my own experience I will say that without practice you cannot understand such things :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question