Skip to content

Instantly share code, notes, and snippets.

@beisong7
Last active March 21, 2023 14:08
Show Gist options
  • Save beisong7/c7ceb1e36c804786f36546e166c15a38 to your computer and use it in GitHub Desktop.
Save beisong7/c7ceb1e36c804786f36546e166c15a38 to your computer and use it in GitHub Desktop.
Excel import to collection to enable search within the excel (Laravel)

how to use

  • ensure that the first row has all the keys you want

  • you can search with the example below

  $items = $collection->where('foo', 'bar');
  $items->all();
  
  • this will return all the items where 'foo' had the value 'bar'
<?php
$headers = array();
$raw_data = array();
foreach($client[0] as $pos=>$row){
if($pos === 0){
//get keys
$headers = $row;
}else{
// dd($row, $headers);
$item = array();
foreach($headers as $key=>$val){
$item[$val] = $row[$key];
}
array_push($raw_data, $item);
}
}
$collection = collect($raw_data);
return $collection;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment