我有一个laravel项目,我不知道这里的错误在哪里。
我有一个产品表,其中包含" low_stock_warning "整数字段和" units_stock "整数字段。
我希望使用" units_stock "小于" low_stock_warning ",所以我有
$stock_warning = Product::whereRaw('units_stock <= low_stock_warning')
->select('id')
->get();
结果mysql是:
select `id` from `products` where units_stock <= low_stock_warning
如果我直接在我的数据库中执行它,我会得到一个产品。好的,但Eloquent告诉我, $ stock_warning 是一个空数组......
我在这里失踪了什么?
谢谢!
编辑1:
我试过了:
$stock_warning = Product::whereColumn('units_stock', '<=', 'low_stock_warning')->pluck('id');
print_r($stock_warning);
结果是:
Illuminate\Support\Collection Object ( [items:protected] => Array ( ) )
编辑2: 这是数据库结构:
--------------
DEVELOPMENT ENVIRONMENT
--------------
+-----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| productdescriptioneng | text | YES | | NULL | |
| units_stock | int(11) | YES | | NULL | |
| low_stock_warning | int(11) | YES | | NULL | |
+-----------------------+------------------+------+-----+---------+----------------+
--------------
PRODUCTION ENVIRONMENT
--------------
+-----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| productdescriptioneng | text | YES | | NULL | |
| units_stock | int(11) | YES | | NULL | |
| low_stock_warning | int(11) | YES | | NULL | |
+-----------------------+------------------+------+-----+---------+----------------+
开发: - 服务器版本:5.7.21 MySQL社区服务器(GPL)
SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
生产: - 服务器版本:5.7.21-0ubuntu0.16.04.1(Ubuntu)
SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
答案 0 :(得分:1)
使用whereColumn()
方法:
Product::whereColumn('units_stock', '<=', 'low_stock_warning')->pluck('id')
答案 1 :(得分:0)
验证DB中的产品是否未被软删除(deleted_at不为空); 如果它被软删除,但你需要它们使用它:
Product::whereColumn('units_stock', '<=', 'low_stock_warning')->withTrashed()->pluck('id');
答案 2 :(得分:0)
您可以使用WhereColumn和orWhereColumn来实现。
Product::whereColumn('units_stock', '<', 'low_stock_warning')
orWhereColumn('units_stock', '=', 'low_stock_warning')
->pluck('id');
希望这有帮助