R
R
Roman2019-02-18 04:50:23
Laravel
Roman, 2019-02-18 04:50:23

If you need to get data from many models in one Laravel controller, what is the best way to group the code?

Hello.
Suppose I need to receive and send data from the Car, Wheel, Glass, Door models in the IndexController to the view - how is it customary to do:
- Just receive all the data in one function and send it to the view?
- Or write a separate function of the getCars type for each model - and then call these functions inside the method that is registered in the router, and still send them to the view?
How do you usually do this and why?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-02-18
@procode

If the code is not repeated, then in one method and send to the view.
If it repeats within the controller, I put it in a separate method

public function getUsers()
{
     return User::all();
}

And then I get it in any method .
If it repeats within the application, I take it out in traits . This is convenient. you change in one place, not in 10.
If the code is not complex or changes are not expected, I can’t stand it anywhere, even if it repeats.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question