php - 为什么 laravel 查询不返回正确的结果?

标签 php mysql laravel-4


我在 laravel 中有这段代码来获取即将用完的产品。

$productos = DB::table('productos')
                  ->where('producto_minimo', '>=', 'producto_cantidad')
                  ->get();

我得到的是以下结果

Laravel Result

这不是正确的结果。在 MySql 中,通过此查询 SELECT * FROM productos where producto_minimo >= producto_cantidad;

MySql Result

更新
查询日志 - DB::getQueryLog() - 显示了这一点

  2 => 
    array (size=3)
      'query' => string 'select * from `productos` where `producto_minimo` >= ?' (length=54)
      'bindings' => 
        array (size=1)
          0 => string 'producto_cantidad' (length=17)
      'time' => float 1

最佳答案

我假设您必须使用 whereRaw 方法:

$productos = DB::table('productos')
                  ->whereRaw('producto_minimo >= producto_cantidad')
                  ->get();

您的查询会将 producto_minimo 列中的值与字符串 'producto_cantidad' 进行比较

查看 advanced wheres 的 Eloquents 文档:

DB::table('users')
        ->whereExists(function($query)
        {
            $query->select(DB::raw(1))
                  ->from('orders')
                  ->whereRaw('orders.user_id = users.id');
        })
        ->get();

The query above will produce the following SQL:

select * from users
where exists (
    select 1 from orders where orders.user_id = users.id
)

关于php - 为什么 laravel 查询不返回正确的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122554/

相关文章:

javascript - 用于验证苹果包标识符或苹果 ID 的正则表达式

php - 相当于 $_SERVER ['DOCUMENT_ROOT' ] 当 cron 调用脚本时会起作用吗?

mysql - phpMyAdmin 是否具有相当于 CTRL + Z 的功能?

Laravel 4 - 隐式 Controller 中的 URI 参数

php - 处理 PHP/MySQL 中的未读帖子

php - 在生成 HTML 输出之前从 php 中的外部 javascript 获取 cookie

php - 将数据库值与另一个表中的行数进行比较

mysql - 构建一个动态的 no-fail where 子句

php - jason lewis basset 在 laravel 4 中没有很好地显示图标

php - Laravel4 : upload a photo