samedi 3 novembre 2018

I am trying to upload a file but my syntax has some errors

I am trying to upload an excel file using https://www.youtube.com/watch?v=6P_nqOX38CE but i am getting this error...

array_combine(): Both parameters should have an equal number of elements

I don't know how to go about it because i am new to laravel. I would also appreciate if anyone had a better suggestion on creating an excel upload functionality in my laravel project This is my controller...

public function parseImport(Request $request)
        {

            //get file
        $upload=$request->file('csv_file');
        $filePath=$upload->getRealPath();
                //open and read
        $file=fopen($filePath, 'r');
        $header= fgetcsv($file);
            // dd($header);
    $escapedHeader=[];
    //validate
    foreach ($header as $key => $value) {
        $lheader=strtolower($value);
        $escapedItem=preg_replace('/[^a-z]/', '', $lheader);
        array_push($escapedHeader, $escapedItem);
    }
    //looping through othe columns
    while($columns=fgetcsv($file))
    {
        if($columns[0]=="")
        {
            continue;
        }
        //trim data
        foreach ($columns as $key => &$value) {
            $value=preg_replace("/[^A-Za-z0-9?! ]/","",$value);
        }
       $data= array_combine($escapedHeader, $columns);


       // Table update
       $stud=$data['student_id'];
       $fname=$data['first_name'];
       $midname=$data['middle_name'];
       $surname=$data['surname'];
       $gender=$data['gender'];
       $class=$data['class'];
       $stream=$data['stream'];

       $budget->save();

            }

}

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire