Cycs
Cycs

Reputation: 239

eloquent ORM controller or model

I am writing a web application, and I am a self proclaimed disorganised developer, I write things quickly and worry about maintenance later. I am working with laravel at the moment, I have quite alot of database interaction, all the examples of eloquent seem to interacting with database object directly in the controller. Is this the best option, or is it more organised to wrap these eloquent methods in a function in the Model for that relevant query. e.g.

class HomeController extends \BaseController {

    public function index()
    {
         $user = User::find(1);
         return $user;
    }

}

would this be better served as,

class HomeController extends \BaseController {

    public function index()
    {
         $user = new User;
         $result = $user->getSingleUser(1); //Being a method in the User.php model
         return $result;
    }

}

I realise this is very very basic example, but for organisational purposes is it best to seperate the database and "business logic" away from the controller?

Upvotes: 1

Views: 828

Answers (2)

dgierejkiewicz
dgierejkiewicz

Reputation: 103

That's why MVC pattern is for. Store your db requests deep inside, also you can separate your model layer from db layer if you wish. If you more decouple your application structure then more manageable is your code.

Upvotes: 0

Laurence
Laurence

Reputation: 60038

but for organisational purposes is it best to seperate the database and "business logic" away from the controller?

Yes

would this be better served as,

Yes - you should separate your logic into the correct areas, to make the code more manageable.

Upvotes: 1

Related Questions