vendredi 19 février 2021

Laravel Eloquent Collection Where Not Working As Expected

I am unable to know why filtering the retrieving collection does not provide me the data. Could anybody explain why this particular issue occurred?

$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
}

Filtering in the collection does not provide me the Model Instance, whereas Querying inside a loop provides me the Model Instance. I want to optimize the query, so I previously retrieve all products.

Information:

Product Count = 743

Product Id found for first loop = 12

Collection $collection Count = 1486



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire