Skip to content
Advertisement

I’m working at laravel project but there is always that error : ErrorException Creating default object from empty value

this is my candidatController.php the error is in the line before the last line($prev_canditure->etat="archifé";)

$candidat_nw=Candidat::orderby('created_at','DESC')->first();
$prev_canditure=Candidature::where('candidat_id','=',$candidat->id)->orderby('created_at', 'DESC')->first();

$prev_canditure->etat = "Archivé";
$prev_canditure->save();

Advertisement

Answer

From your code you can use a defensive code approach by checking if the query will return null

$candidat_nw=Candidat::orderby('created_at','DESC')->first();
$prev_canditure=Candidature::where('candidat_id','=',$candidat->id)->orderby('created_at', 'DESC')->first();

if (!(empty($prev_candidature))){
$prev_canditure->etat = "Archivé";
$prev_canditure->save();

}


User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement