[英]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 image
或system 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.