【发布时间】:2021-05-22 06:49:54
【问题描述】:
我不知道为什么过滤检索集合不会为我提供数据。谁能解释为什么会出现这个问题?
$products = Product::get();
//$collection is retrive from the excel
foreach($collections as $collection){
// Scenario 1:
$product = $products->where('product_name', $collection[0])->first();
dd($product); // Returns null
// Scenario 2
$product = Product::where('product_name', $collection[0])->first();
dd($product); // It provides me the Product Model instance
}
在集合中进行过滤不会为我提供模型实例,而在循环内进行查询为我提供了模型实例。我想优化查询,所以我之前检索了所有产品。
信息:
产品数量 = 743
为第一个循环找到的产品 ID = 12
收藏 $collection Count = 1486
【问题讨论】:
-
看起来很奇怪,我检查了相同的代码并且它有效...您是否检查了集合
producsts包含具有这样名称的元素? -
是的。问题是因为不区分大小写。在Model过滤器中,没关系;而在 Collection 过滤器中,区分大小写确实会影响。
标签: php laravel laravel-5 eloquent