D
D
David2016-07-06 12:23:40
Laravel
David, 2016-07-06 12:23:40

Eloquent. Show request without "?", but with substituted values?

Hello. How to show query in Eloquern without "?", but with substituted values?
Those. if you display a query using the toSql() function, then instead of the substituted where values, it indicates ?.
For example, I have a request that is correctly formed, but for some reason does not work.
Works:

select * 
from `users` 
inner join `pro_payments` 
  on `users`.`id` = `pro_payments`.`user_id` 
    and (`pro_payments`.`start` < NOW() 
         and `pro_payments`.`end` > NOW() 
         and `pro_payments`.`active` = 1
    ) 
where `group_id` = 2 
and `users`.`deleted_at` is null

In ORM:
public function scopeWithPayment($query){
        return $query->join('pro_payments', function($join){
            
            $join->on('users.id', '=', 'pro_payments.user_id')
                ->where(function($sql){
                    $sql->where('pro_payments.start', '<', DB::raw('now()'))
                        ->where('pro_payments.end', '>', DB::raw('now()'))
                        ->where('pro_payments.active', '=', 1);
                });
        });
}
public function scopeProviders($query){
        return $query->where('group_id', 2);
}

As a result (not working):
User::providers()->withPayment()->toSql()
select * from `users` inner join `pro_payments` on `users`.`id` = `pro_payments`.`user_id` and (`pro_payments`.`start` < ? and `pro_payments`.`end` > ? and `pro_payments`.`active` = ?) where `group_id` = ? and `users`.`deleted_at` is null

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
D', 2016-07-06
@MiragePresent

You can write DB::enableQueryLog() before the request, and after the request do dd(DB::getQueryLog())
This will show all requests + data that were used (the data will be separate).

D
Dmitry Evgrafovich, 2016-07-06
@Tantacula

You can also install Laravel Debugbar - a very convenient debugger and not only for sql queries.

Y
YURGEN GOA, 2016-07-06
@yurgengoa

alternatively watch all requests via
https://github.com/itsgoingd/clockwork-chrome

V
Vyacheslav Plisko, 2016-07-06
@AmdY

And why don't you want to use ORM, but just use a Queri Builder from it? It would return you a beautiful object, not game after join.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question