繁体   English   中英

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

[英]check in blade if exists variable in @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 ,然后发送到我的侧边栏。

我正在尝试这样做:

主页.blade.php

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

头刃.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 

这个回报:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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