如果@include 中存在变量,则检查刀片

问题描述

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

我的控制器中有此代码

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

此返回 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',"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 

这次返回:

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

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

解决方法

你可以使用if条件

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