Laravel Blade set ternary if value on input
P粉322106755
P粉322106755 2024-03-19 16:25:08
0
2
406

I have a template that changes the style if the input has the "value" attribute set, even if it is empty.

For example:

<input type="text" class="form-control" name="name" id="name" value="">

The template is different from this:

<input type="text" class="form-control" name="name" id="name">

So I need to use a ternary if to check if the value is set.

I wrote this code:

<input type="text" class="form-control" name="name" id="name" {{ (isset($category) ? ("value='".$category->name ."'") : "") }}>

But when I run the page and set the $category variable, I find the following value in the input: 'Category

I checked the generated code and I can find this:

<input type="text" class="form-control" name="name" id="name" value="'Category" a="" name'="">

But the value in DB is Category Name.

Can you help me find the problem?

P粉322106755
P粉322106755

reply all(2)
P粉947296325

You just need to do this;

P粉978742405

I don't think the ternary operator is needed in this case because you won't do anything in case the condition results in false.

You can just use @if:

@if (isset($category)) value="{{$category->name}}" @endif

Also, there is a @isset directive, so it might be more appropriate if you do this:

@isset($category) value="{{$category->name}}" @endisset

In your input


Reference: Blade InstructionsIf Statement.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!