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粉9787424052024-03-20 10:21:38
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.