php Blade if(isset) 不工作 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28497538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:53:36  来源:igfitidea点击:

Blade if(isset) is not working Laravel

phplaravel

提问by WahidSherief

Hi I am trying to check the variable is already set or not using blade version. But the raw php is workingbut the blade version is not. Any help?

嗨,我正在尝试检查变量是否已设置或未使用刀片版本。但是原始 php 正在运行,刀片版本不是。有什么帮助吗?

controller:

控制器:

public function viewRegistrationForm()
{
    $usersType = UsersType::all();
    return View::make('search')->with('usersType',$usersType);
}

view:

看法:

{{ $usersType or '' }}

it shows the error :

它显示错误:

Undefined variable: usersType (View: C:\xampp\htdocs\clubhub\app\views\search.blade.php)

未定义变量:usersType(视图:C:\xampp\htdocs\clubhub\app\views\search.blade.php)

回答by lukasgeiter

{{ $usersType or '' }}is working fine. The problem here is your foreach loop:

{{ $usersType or '' }}工作正常。这里的问题是你的 foreach 循环:

@foreach( $usersType as $type )
    <input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span> &nbsp; 
@endforeach

I suggest you put this in an @if():

我建议你把它放在一个@if()

@if(isset($usersType))
    @foreach( $usersType as $type )
        <input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span> &nbsp; 
    @endforeach
@endif

You can also use @forelse. Simple and easy.

您也可以使用@forelse。简单易行。

@forelse ($users as $user)
   <li>{{ $user->name }}</li>
@empty
   <p>No users</p>
@endforelse

回答by Bhargav Kaklotara

@isset($usersType)
  // $usersType is defined and is not null...
@endisset

For a detailed explanation refer documentation:

有关详细说明,请参阅文档

In addition to the conditional directives already discussed, the @issetand @emptydirectives may be used as convenient shortcuts for their respective PHP functions

除了已经讨论过的条件指令之外,@isset@empty指令可以用作各自 PHP 函数的便捷快捷方式

回答by nXu

Use 3 curly braces if you want to echo

如果要回显,请使用 3 个花括号

{{{ $usersType or '' }}}

回答by PriyankMotivaras

Use ?? , 'or' not supported in updated version.

用 ??, 'or' 不支持更新版本。

{{ $usersType or '' }}  ?
{{ $usersType ?? '' }} ?

回答by kamran

You can use the ternary operator easily:

您可以轻松使用三元运算符:

{{ $usersType ? $usersType : '' }}

回答by bad4iz

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <p>No users</p>
@endforelse

回答by fred

I solved this using the optional()helper. Using the example here it would be:

我使用optional()助手解决了这个问题。使用此处的示例将是:

{{ optional($usersType) }}

{{ optional($usersType) }}

A more complicated example would be if, like me, say you are trying to access a property of a null object (ie. $users->type) in a view that is using old()helper.

一个更复杂的例子是,如果像我一样,假设您试图访问$users->type使用old()helper的视图中的空对象(即)的属性。

value="{{ old('type', optional($users)->type }}"

value="{{ old('type', optional($users)->type }}"

Important to note that the brackets go around the object variable and not the whole thing if trying to access a property of the object.

重要的是要注意,如果尝试访问对象的属性,括号会围绕对象变量而不是整个对象。

https://laravel.com/docs/5.8/helpers#method-optional

https://laravel.com/docs/5.8/helpers#method-optional

回答by Connectify_user

On Controller

在控制器上

$data = ModelName::select('name')->get()->toArray();
return view('viewtemplatename')->with('yourVariableName', $data);

On Blade file

在刀片文件上

@if(isset($yourVariableName))
//do you work here
@endif

回答by Qu?c Tu?n D??ng

Use ??instead or {{ $usersType ?? '' }}

使用??替代或{{ $usersType ?? '' }}