Advanced consultation with Relationships in laravel

1

Good, basically what I want to generate is a query that returns a list of users, that have as excacto value the result of the difference of two fields.

In other words, I have the user table that has the fields ndias and diasconsumidos.

CURRENTLY I am doing the following

User::where('ndias','-','diasconsumidoss','=','5')->get();

but this does not work.

What I want you to do is basically the following. I leave the example of how the SQL query should be

SELECT * FROM 'users' WHERE (users.ndias - users.diasconsumidos) =5
    
asked by FuriosoJack 10.05.2017 в 00:35
source

2 answers

2

The best thing in this case would be to use whereRaw . The code would be like this:

User::whereRaw('(ndias - diasconsumidos) = 5')->get();

Also, if you want you can pass the value 5 as a parameter in this way:

$myVar = 5;
User::whereRaw('(ndias - diasconsumidos) = ?', [$myVar])->get();
    
answered by 10.05.2017 / 00:45
source
1

You can use whereRaw

User::whereRaw('(users.ndias - users.diasconsumidos) =5')->get()
    
answered by 10.05.2017 в 00:42