3

Quick question, i am studying Laravel 4 as i am coming from Codeigniter, i noticed that some laravel tutorials have their queries in the controller and some have their queries in the model.

My question is, where should we write all of our queries for best practice? In the controller or the model? This is regarding to large web applications.

Many thanks

0

1 Answer 1

2

Best practice is to write your queries in a separate layer. So separate from your controller, and separate from your model. Controller best practice also depends on the scale of your project and your specific demand for maintainability.

As soon as you scale up the project, try to extract more so maintainability is less of an issue. A layer in between for queries scoped around the User model can look something like:

class UserRepository {

    public function __construct(Illuminate\Database\DatabaseManager $db)
    {
        $this->db = $db;
    }

    public function getLatestUsers($limit = 5) {
        //$this->db->table('users')->
    }

}

For more best practices, you should take a look at all the resources that are being created around laravel. Like

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, I have never seen that method before. Say if I do create the UserRepository class, where would I store it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.