Laravel条件问题

时间:2018-10-22 14:31:03

标签: laravel where

这是我的查询字符串:

select `sku_alias`, `use_from_date`, `use_until_date` 
from `catalog_alias` 
where (`sku` = ? and `warehouses_id` = ?) and 
((date(`use_from_date`) <= NOW() and date(`use_until_date`) >= NOW() or `use_until_date` is null)) 
limit 1

根据以下代码生成:

$whereCondition = [
    ['sku', '=', $sku],
    ['warehouses_id', '=', $wareHouseId]
];
$item = Alias::select('sku_alias', 'use_from_date','use_until_date')
    ->where($whereCondition)
    ->where(function($query){
        $query->where(function($query_2){
            $query_2->whereDate('use_from_date', '<=', DB::raw('NOW()'))->whereDate('use_until_date', '>=', DB::raw('NOW()'));
            $query_2->orWhereNull('use_until_date');
        });
    })->first();

$ item的结果是NULL,我的$sku'XXXXXXX',它是一个字符串,我认为我的条件类似于 where ( sku = XXXXXXX and ....,由于缺少'',它将以mysql错误结束。

如何解决此问题?我应该如何更改此数组:['sku', '=', $sku],

谢谢

更新:请参见mysql错误: enter image description here

如果我使用直接查询:

$item = DB::select("select
      `sku_alias`,
      `use_from_date`,
      `use_until_date`
    from `catalog_alias`
    where (`sku` = '{$sku}' and `warehouses_id` = '{$wareHouseId}') and
          ((date(`use_from_date`) <= NOW() and date(`use_until_date`) >= NOW() or `use_until_date` is null))
    limit 1;");  

它有效,只是好奇为什么第一类查询不起作用?

0 个答案:

没有答案