Laravel查询构建器返回空,其中phpmyadmin返回结果

时间:2016-12-13 13:34:46

标签: php laravel laravel-5 query-builder

我正在尝试解决使用Laravel的查询构建器的问题我没有得到任何结果,但是当我在php中运行相同的查询我的管理员时我得到了结果..

这是查询:

DB::table('nm_product')->select(
    DB::raw(
        'IF(LOCATE(" ",pro_title,POSITION("'.$products
        .'" IN pro_title))<>"0",SUBSTR(pro_title,POSITION("'.$products
        .'" IN pro_title),LOCATE(" ",pro_title,POSITION("'.$products
        .'" IN pro_title)) - POSITION("'.$products
        .'" IN pro_title) ) , "aaa")  as keyword'
    ),
    'mc_name',
    'pro_title'
)->join('nm_maincategory', 'mc_id', '=', 'pro_mc_id'
)->where(DB::raw('LOWER(pro_title)'),'like','"% '.$products.'%"'
)->orwhere(DB::raw('LOWER(pro_title)'),'like','"'.$products.'%"'
)->get();

2 个答案:

答案 0 :(得分:0)

您可以看到查询正在运行,它是一种测试流程的方式:

#Last Query
\DB::enableQueryLog();

// code request
DB::table('nm_product')->select(...

$query = \DB::getQueryLog();
$lastQuery = end($query);
\Log::info(json_encode($lastQuery));

示例:

你得到这样的东西

array:3 [
  "query" => "select * from `table` where `table`.`id` = ?"
  "bindings" => array:1 [▼
    0 => 1
  ]
  "time" => 0.5
]

在查询中替换绑定的值,您可以直接将输出运行到数据库以查看查询是否正确

Select * from `table` where `table`.`id` = 1

答案 1 :(得分:0)

    [1] => Array
            (
                [query] => select IF(LOCATE(" ",pro_title,POSITION("can" IN pro_title))"0",SUBSTR(pro_title,POSITION("can" IN pro_title),LOCATE(" ",pro_title,POSITION("can" IN pro_title)) - POSITION("can" IN pro_title) ) , "aaa")  as keyword, `mc_name`, `pro_title` from `nm_product` inner join `nm_maincategory` on `mc_id` = `pro_mc_id` where LOWER(pro_title) like ? or LOWER(pro_title) like ?
                [bindings] => Array
                    (
                        [0] => "% can%"
                        [1] => "can%"
                    )

                [time] => 0.52
            )

我替换了变量并将查询放在phpmyadmin中,我得到了这个结果

Display Image