简体   繁体   中英

check in blade if exists variable in @include

I´m developping a system with laravel 7.4 and i need check if one variable that i´m sending from my controller exists in my blade. If exists, include it:

I have this code in my controller:

public function index()
    {
        //Store Image
        $newsItem = User::find(Auth::user()->id);

        if($newsItem->hasMedia('userPhoto')){
            $profilePhoto = $newsItem->getMedia('userPhoto')[0]->file_name;
            $idPhoto = $newsItem->getMedia('userPhoto')[0]->id;

            return view('home')->with('idPhoto', $idPhoto)
                               ->with('profilePhoto', $profilePhoto);

        }else if($newsItem->hasMedia('logoSystem')){
            $logoSystem = $newsItem->getMedia('logoSystem')[0]->file_name;
            $idPhotoLogo = $newsItem->getMedia('logoSystem')[0]->id;

            return view('home')->with('idPhotoLogo', $idPhotoLogo)
                               ->with('logoSystem', $logoSystem);
        }else{
            return view('home');
        }
        
        if($newsItem->getMedia('userPhoto') && $newsItem->getMedia('logoSystem')){
            return view('home')->with('idPhoto', $idPhoto)
                                ->with('profilePhoto', $profilePhoto)
                                ->with('idPhotoLogo', $idPhotoLogo)
                                ->with('logoSystem', $logoSystem);
        }
        
    }

this return profile image or system image but it´s possible that this images don´t exists and i need check before, because i have error in my system and i can´t show my view.

Whit this code in my view after, i created a image with this route:

{{-- asset('storage/'.$idPhoto."/".$profilePhoto) --}}

But previously i need send this variable ton my view head and after to my sidebar.

i´m traying to do this:

home.blade.php

@include('layouts.head', (isset([$idPhoto])) ? ["idPhoto" => $idPhoto, "idPhotoLogo" => $idPhotoLogo]  : '')

head.blade.php

@include('layouts.sidebar', ["idPhoto", $idPhoto, "idPhotoLogo" => $idPhotoLogo])

and in my sidebar created my image:

<img src="{{-- asset('storage/'.$idPhoto."/".$profilePhoto) --}}" alt="Logo" class="brand-image">

UPDATED

now i have this error:

in my head.blade.php i have this:

@if(isset([$idPhoto])
              @include('layouts.sidebar', ["idPhoto", $idPhoto, "idPhotoLogo" => $idPhotoLogo])
            @else
              @include('layouts.sidebar')
            @endif

in my home.blade.php i have this

@if(isset([$idPhoto])
    @include('layouts.head',["idPhoto" => $idPhoto, "idPhotoLogo" => $idPhotoLogo])
@else
    @include('layouts.head')
@endif 

this return:

syntax error, unexpected token ":", expecting "(" (View: C:\wamp64\www\clinicaCampoy\resources\views\home.blade.php)

i need to do, that if table media is empty, load image for deafult, but if table media have logo or image system, load this images

you can use if condition

@if(isset($idPhoto))
    @include('layouts.head',["idPhoto" => $idPhoto, "idPhotoLogo" => $idPhotoLogo])
    
@endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM