Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do we have to write queries in Laravel: in model, controller or routes?

I'm little confused with the database queries in the laravel, where do we have to write our queries: in controllers, models or routes ?

I've been through many tutorials and I see so much difference. Just creating confusion.

Kindly explain somebody

like image 734
anoop Singh Avatar asked Dec 24 '15 08:12

anoop Singh


People also ask

What is the difference between model and controller in Laravel?

In clear terms: Model performs all operations on data from DB. Controller call necessary model methods and ready the data.

What is the use of controller in Laravel?

Controllers can group related request handling logic into a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users. By default, controllers are stored in the app/Http/Controllers directory.

What are routes in Laravel?

The route is a way of creating a request URL for your application. These URLs do not have to map to specific files on a website, and are both human readable and SEO friendly. In Laravel, routes are created inside the routes folder. They are are created in the web. php file for websites.

What is query () in Laravel?

In Laravel the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a champ.


1 Answers

It depends on different factors, but in short you can write them both in Model, Controllers or Repositories

If you're writing a controller action and you need a query that you'll use only once, it's perfectly fine to write the query directly in the controller (or even in the route's closure ).

For example, if you want to get all users of type admin:

$admins = User::where('type', 'admin')->get();

Now, suppose you need to get the admins in more than one controller method; instead of rewriting the same query, you can create a Repository class to wrap the access to the users' Model and write the query inside the Repository:

class UserRepository
{
    public function getAllAdmins()
    {
        return User::where('type', 'admin')->get();
    }
}

Now in your controllers you can inject the Repository and use the same method of the Repository to get the admin users: this will keep your code DRY as you don't have to repeat the same query among the controllers' actions

Controller

public function __construct(UserRepository $userRepo)
{
     $this->userRepo = $userRepo;
} 

//controller action
public function index()
{
     $admins = $this->userRepo->getAllAdmins(); 
}

Finally, let's suppose you need a query to count the number of the admin users. You could write this query in the UserRepository:

public function getAdminNum()
{
    return User::where('type', 'admin')->count();    
}

And it would be ok, but we can note that the User::where('type', 'admin') fragment of the query is shared with the query in getAllAdmins So we can improve this by using query scopes :

User Model

public function scopeAdmins($query)
{
    return $query->where('type', 'admin');    
}

by this, in the UserRepository methods we can rewrite our previous queries as:

public function getAllAdmins()
{
    return User::admins()->get();
} 

public function getAdminNum()
{
    return User::admins()->count();  
}

And i've just showed you a case in which a query would be writed inside a Model

like image 110
Moppo Avatar answered Oct 10 '22 10:10

Moppo