很多时候我们想得到最后一条数据库语句,那么php - laravel 5.1 如何打印最后一次执行的sql ,laravel ORM怎么打印出执行sql语句呢


model内部方法内容:
/**
* 根据条件查询
*/
public function getDataByCondition($condition = [])
{
$this->getconnection()->enableQueryLog(); //开启日志

$_m = $this->select('id', 'b', 's', 'c', 'type', 'tid');
if (!empty($condition)) {
$_m =  $_m->where($condition);
   }
$_m = $_m->whereRaw('UNIX_TIMESTAMP(endtime) > '.time());
$res = $_m->orderBy('b', 'ASC')->orderBy('id', 'ASC')->paginate(self::PAGE_NUMS);

print_r($this->getconnection()->getQueryLog());exit; //打印sql

return $res;
}

打印结果:
Array
(
    [0] => Array
        (
            [query] => select count(*) as aggregate from `car` where (`type` <> ?)
             and UNIX_TIMESTAMP(endtime) > 1475044311 and `car`.`deleted_at` = 0
            [bindings] => Array
                (
                    [0] => 2
                )

            [time] => 93.33
        )

    [1] => Array
        (
            [query] => select `id`, `b`, `s`, `c`, `type`, `tid` from `car` where (`type` <> ?) 
            and UNIX_TIMESTAMP(endtime) > 1475044311 and `car`.`deleted_at` = 0 order by `b` asc, `id` asc limit 15 offset 0
            [bindings] => Array
                (
                    [0] => 2
                )

            [time] => 80.7
        )

)

网络资源:
以下操作在Dao层操作    
public function lists($ids, $columns)     
 {          
    $this->getconnection()->enableQueryLog(); //开启日志            
    if(!empty($ids)) {              
        $data = $this->whereRaw('id=367 OR id=176')->get($columns);                
        print_r($this->getconnection()->getQueryLog());exit; //打印sql                
        return  $data = !empty($data) ? $data->toArray() : [];          
    }      
}


本文链接二维码可以保存在本地:保存

| 浏览 (5567) | (0) | (0) | | 2016-11-07 22:46:05 |


相关文章

  1. Mac安装Swoole扩展phpize 时 Cannot find autoconf 解决方法
  2. mac下安装swoole扩展报错PHP Startup Unable to load dynamic library
  3. Laravel 数据库事务
  4. PHP贷款等额本息、等额本金计算月供还款计划公式
  5. Laravel 关闭过滤排除部分路由VerifyCsrfToken 验证
  6. Laravel Horizon监控Redis队列queue