简体   繁体   English

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

[英]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.我正在使用 laravel 7.4 开发一个系统,我需要检查我从控制器发送的一个变量是否存在于我的刀片中。 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.此返回profile imagesystem image但此图像可能不存在,我需要事先检查,因为我的系统有错误,无法显示我的视图。

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.但以前我需要将此变量发送到我的视图head ,然后发送到我的侧边栏。

i´m traying to do this:我正在尝试这样做:

home.blade.php主页.blade.php

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

head.blade.php头刃.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:在我的head.blade.php我有这个:

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

in my home.blade.php i have this在我的home.blade.php我有这个

@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 条件

@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