Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Wolf9908's avatar

Validate date in a spreadsheet before import (maatwebsite laravel-excel)

Hello,i'm trying to validate a date before import the infos to the database,i need to check if there's a date above today in the file but for some reason(might be a stupid mistake) my foreach loop only check the first index of the array and not all of them,i've searched everywhere for a solution but i couldn't find anything... My import function:

public function Import(Request $request){
    $count = 0;
    $today_carbon = Carbon::today()->toDateString();
    $request->validate(['file'=>'required|mimes:csv']);
    $file   = $request->file('file');
    $spreadsheet = Excel::toArray(new UserImport,$file);
    foreach($spreadsheet as $p){
        foreach($p as $i){
            $count++;
            $columns= count($i);
            $date_file = $i[2];
            if($today_carbon >= $date_file){
                Excel::import(new UserImport,$file);
                return redirect('form')->with('success','ok');
            }else{
                return redirect('form')->with('erro','error');
            }
            if($columns == 3){
                Excel::import(new UserImport,$file);
                return redirect('form')->with('success','ok');
            }else{
                return redirect('form')->with('erro','error');
            }
        }
    }
}

my import model function:

public function model(array $row){
    
        return new User([
            'name'    => $row[0], 
            'email' => $row[1],
            'sell_date' => $row[2]
        ]);  
}
0 likes
6 replies
Snapey's avatar

you are using return, when perhaps you mean continue

also, this section

            if($columns == 3){
                Excel::import(new UserImport,$file);
                return redirect('form')->with('success','ok');
            }else{
                return redirect('form')->with('erro','error');
            }

is never executed because of the if...else in the section above

1 like
Wolf9908's avatar
public function Import(Request $request){
    $count = 0;
    $today_carbon = Carbon::today()->toDateString();
    $request->validate(['file'=>'required|mimes:csv']);
    $file   = $request->file('file');
    $spreadsheet = Excel::toArray(new UserImport,$file);
    foreach($spreadsheet as $p){
        foreach($p as $i){
            $count++;
            $columns= count($i);
            $date_file = $i[2];
            if($today_carbon >= $date_file){
                continue;
            }
            if($columns == 3){
                Excel::import(new UserImport,$file);
                return redirect('form')->with('success','ok');
            }else{
                return redirect('form')->with('erro','error');
            }
        }
    }
}

like this? because doesn't worked for me :(

Snapey's avatar

what's the point of the foreach loop if you are going to return in the middle of it?

1 like
Wolf9908's avatar

Both foreach loops is to access manually the infos of the spreadsheet on the array,the second foreach is to access the fields of each column of the spreadsheet on the array.If you dd($spreadsheet); will show you something like this :

array:12 [▼ 0 => array:3 [▼ 0 => "Jason" 1 => "[email protected]" 2 => "2020-07-09" ] 1 => array:3 [▶] 2 => array:3 [▶]

Snapey's avatar

but you are not accessing EACH of the fields. You are returning from the function first time you reach a return statement

Wolf9908's avatar

But in my foreach loops returns my array as i show you while debbuging right? then how i do validate them?

Please or to participate in this conversation.