DoppyWoppy's avatar

Call to a member function keys() on null

I am trying to get count of header from an uploaded CSV file. But I am getting error "Call to a member function keys() on null". Here is my code.

public static function import(request $request) { $error = [];

if($request->hasFile('file'))
{
    
    $path =  $request->file->getRealPath();
    $data = Excel::load($path)->first()->keys()->toArray();
    $countHeader = count($data);
    echo $countHeader;
    return;
    if($countHeader <= 10 )
    {
        echo 
        $error =  "file header are less than 10";
    }
    else
    {
        $error = "file headers is okay";
    }
    

} else { $error = "Please select file"; } return $error;

} thanks

0 likes
2 replies
mushood's avatar

Please do post your form code

Ensure that you have enctype

<form action="/url" method="post" enctype="multipart/form-data">

If this is not present, it wont allow file uploads.

Also try to var_dump($path) to see if the $path actually has something

DoppyWoppy's avatar
DoppyWoppy
OP
Best Answer
Level 1

I had a CSV with headers only. that's why it was giving an error. I wrote some data into that CSV file and it works fine.

Please or to participate in this conversation.