Error creating php conditional

Asked

Viewed 36 times

0

I am creating a command in Laravel(php) that looks for an image file that is blob in the database and saved in a folder, And as not all users have image I want to make a conditional that if the file does not exist it simply skips the process but when I put the user who has no photo it does not skip the process and returns Trying to get Property 'logo_foto' of non-object

 public function handle()
{
   // $field = $this->ask('Fields?');

    $id = $this->argument('numero');
    $getInfo = Parceiro::where('id', $id)->first();
    if($getInfo->logo_foto) {
        $photo = $getInfo->getRawOriginal('logo_foto');
        $format = Helper_Base64::getFileFormat($photo);
        $randonName = rand(1, 800);
        $path2 = public_path("users/logo/{$randonName}");

        if(!File::isDirectory($path2)){
            File::makeDirectory($path2, 0777, true, true);
        }

        $path =  "$path2/{$randonName}.$format";
        Partner::getLogoUrl($id, false, null, $path);
       // Partner::getLogoUrl($id);
        $getInfo->img_path = $path;
        $getInfo->save();
    }

inserir a descrição da imagem aqui

1 answer

1

You can use the method exists before calling the method first()

1:

$id = $this->argument('numero');
$getInfo = Parceiro::where('id', $id);
if ($getInfo->exists()) {
    // info existe
    $info = $getInfo->first()
} else {
    // info nao existe
}

Other possibilities:

2:

$id = $this->argument('numero');
$getInfo = Parceiro::where('id', $id)->first();
if ($getInfo) {
    // info existe
} else {
    // info nao existe
}

3:

$id = $this->argument('numero');
$getInfo = Parceiro::where('id', $id)->first();
if ($getInfo->isNotEmpty()) {
    // info existe
} else {
    // info nao existe
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.