如果@include 中存在变量,则签入 blade

check in blade if exists variable in @include

我正在用 laravel 7.4 开发一个系统,我需要检查我从控制器发送的一个变量是否存在于我的 blade 中。如果存在,请包含它:

我的控制器中有这段代码:

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);
        }
        
    }

这个 return profile imagesystem image 但是这个图像可能不存在,我需要先检查一下,因为我的系统有错误,我可以不要表达我的观点。

写完这段代码后,我用这条路线创建了一张图片:

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

但之前我需要将这个变量发送到我的视图 head 之后到我的侧边栏。

我正打算这样做:

home.blade.php

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

head.blade.php

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

并在我的侧边栏中创建了我的图像:

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

已更新

现在我有这个错误:

在我的 head.blade.php 我有这个:

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

在我的 home.blade.php 我有这个

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

这个return:

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

我需要做的是,如果 table 媒体为空,则默认加载图像,但如果 table 媒体有徽标或图像系统,则加载此图像

你可以使用if条件

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