Php Laravel何处不在,何处不在一起工作
我有一个函数,涉及到以下几行:Php Laravel何处不在,何处不在一起工作,php,laravel,eloquent,Php,Laravel,Eloquent,我有一个函数,涉及到以下几行: $products_to_ignore = $this->products()->get()->lists('id'); // returns [1,3,5] $products = Product:: whereNotIn('id', $products_to_ignore) ->where('stock_level', '>', 0) ->with('producer') ->get
$products_to_ignore = $this->products()->get()->lists('id'); // returns [1,3,5]
$products = Product::
whereNotIn('id', $products_to_ignore)
->where('stock_level', '>', 0)
->with('producer')
->get();
return $products;
我有一个DB表,沿着下面的线
id product_name stock_level
1 eggs 5
2 cheese 0
3 milk 1
4 cucumber 4
5 pie 0
但是上面的函数返回[]
如果我用
$products = Product::
where('stock_level', '>', 0)
->with('producer')
->get();
return $products;
我将获得[1,3,4]
,如果我使用:
$products_to_ignore = $this->products()->get()->lists('id'); // returns [1,3,5]
$products = Product::
whereNotIn('id', $products_to_ignore)
->with('producer')
->get();
return $products;
我将获得[2,4]
这两个人怎么打得不好?如何获取库存级别大于0且不在数组中的项目
[1,3,5]
?例如,此问题中的第一段代码应返回product[4]
,不在数组中,并且库存水平>0。您有两个条件,其中
条件彼此相邻。也许,和
会使它们协调一致;)
根据API\vendor\laravel\framework\src\illumb\Database\Query\Builder.php第773行:
/**
* Add a "where not in" clause to the query.
*
* @param string $column
* @param mixed $values
* @param string $boolean
* @return \Illuminate\Database\Query\Builder|static
*/
public function whereNotIn($column, $values, $boolean = 'and')
{
return $this->whereIn($column, $values, $boolean, true);
}
啊!肯定注意!虽然这给了我SQL:
select*from'products'where AND'id'not in(3,4,5,6,7,8,16,17,18)'stock_level'>0
,但我很难按正确的顺序排列。。。我真的很笨吗?颠倒了where
s的顺序。现在应该可以正常工作了。无需传递和'
,因为它是默认值。请确保您的数据与示例中的一样,因为代码正常。
/**
* Add a "where not in" clause to the query.
*
* @param string $column
* @param mixed $values
* @param string $boolean
* @return \Illuminate\Database\Query\Builder|static
*/
public function whereNotIn($column, $values, $boolean = 'and')
{
return $this->whereIn($column, $values, $boolean, true);
}