Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel blade "old input or default variable"?

I want to show the old input in input value. If there isn't old input, than show other variable:

value="{{ old('salary_' . $employee->id) or 'Default' }}" 

But when there is no old input, it gives me 1 instead of the default value!

I think the problem has something to do with the concatenation, but I don't know how to fix it!?

like image 590
Jeton Thaçi Avatar asked Nov 11 '15 14:11

Jeton Thaçi


People also ask

What is the use of old in Laravel blade?

The old helper function is used to retrieve an old input item. It is a shortcut to calling the "Illuminate\Http\Request::old" instance method. It accepts a $key argument and a $default argument.

How do you check if a variable is set or not in Laravel blade?

You can use the @isset blade directive to check whether the variable is set or not.

What is the advantage of Laravel blade template?

In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.

What are the two primary benefits of Laravel blade?

Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.


Video Answer


2 Answers

or is a comparison operator in PHP, so your code is evaluating to true, or 1. What you want is a ternary if statement.

As mentioned, or can be used in blade as shorthand for a ternary if statement.

But you can (and should) just pass the default value as the second argument to the function, like so:

value="{{ old('salary_' . $employee->id, 'Default') }}" 
like image 127
Adunahay Avatar answered Sep 25 '22 20:09

Adunahay


You can use the code (for PHP 7):

{{ old('field_name') ?? $model->field_name ?? 'default' }} 

For checkbox checked attribute use the code (if default value is false):

{{ (old() ? old('field_name', false) : $model->field_name ?? false) ? 'checked' : '' }} 

Construction {{ $something or 'default'}} works only for variables

like image 35
Антон Баранов Avatar answered Sep 24 '22 20:09

Антон Баранов