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?
You just need to do this;
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
:Also, there is a
@isset
directive, so it might be more appropriate if you do this:In your input
Reference: Blade InstructionsIf Statement.