因此,我正在製作一個表單,我想讓用戶選擇上傳一個允許表單自動填充的CSV。所以我認爲我會構建一個讀取CSV的函數,然後將每行作爲一個對象拋入一個數組中,然後我可以將其傳回到我的Laravel Blade模板。我唯一的問題是,我從函數返回的數組總是空的。有任何想法嗎?Laravel Excel:CSV到數組
private function import($path) {
$applicants = [];
Excel::load($path, function(LaravelExcelReader $excel) use ($applicants){
$excel->each(function(Collection $line) use ($applicants){
$name = new \stdClass;
$name->first = $line->get('first');
$name->middle = $line->get('middle');
$name->last = $line->get('last');
$name->birthdate = $line->get('birthdate');
$name->ssn = $line->get('ssn');
$name->email = $line->get('email');
$name->mobile_phone = $line->get('mobile_phone');
$name->home_phone = $line->get('home_phone');
$name->street = $line->get('street');
$name->city = $line->get('city');
$name->state = $line->get('state');
$name->zip = $line->get('zip');
array_push($applicants, $name);
});
});
return $applicants;
}