【问题标题】:Laravel query builder returning empty where phpmyadmin returns resultsLaravel 查询生成器在 phpmyadmin 返回结果的地方返回空
【发布时间】:2016-12-14 01:44:19
【问题描述】:

我正在尝试解决使用 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();

【问题讨论】:

    标签: php laravel laravel-5 query-builder


    【解决方案1】:

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

    #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
    

    【讨论】:

      【解决方案2】:
      [1] => 数组 ( [查询] => 选择 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") 作为关键字, `mc_name`, `pro_title` 来自 `nm_product` 内部连接 ​​`nm_maincategory` on `mc_id` = `pro_mc_id` 其中LOWER(pro_title) 喜欢吗?或 LOWER(pro_title) 之类的? [绑定] => 数组 ( [0] => "% 可以%" [1] => "可以%" ) [时间] => 0.52 )

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

      Display Image

      【讨论】:

      • 将结果存储在变量$foo = DB::table('nm_product')-&gt;select(... 中,然后使用dd($foo-&gt;toArray()) 打印出来
      • Fatal Error : Call to a member function toArray() on array
      • 尝试运行 $foo = DB::table('nm_product')-&gt;get(); dd($foo-&gt;toArray());
      • I got the Same Error
      • $foo = DB::table('nm_product')-&gt;get(); dd($foo); 也许你正在使用另一个带有空表的数据库检查你 .env 文件
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2018-08-05
      • 2021-03-18
      • 2021-11-20
      • 2019-10-13
      相关资源
      最近更新 更多