Laravel blade “old input or default variable”?

后端 未结 4 1948
执笔经年
执笔经年 2021-02-06 22:30

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\' }         


        
相关标签:
4条回答
  • Try this:

    <textarea name="alamat" id="" class="form-control" placeholder="Alamat">{{ old('alamat').@$biodata->alamat }}</textarea>
    
    0 讨论(0)
  • 2021-02-06 22:44

    As described in the Laravel doc: "If you are displaying old input within a Blade template, it is more convenient to use the old helper:". So if you need add/edit data form (when you need to use edit form for add and edit in edit mode you need to use loaded data from model (database)) to show values from the model (through controller) you can use following:

    name="some_value" value="{{ $some_value or old('some_value', $the_value) }}"
    

    where is "some_value_from_model" variable name in view array. In this case it should be at first $some_value will be used to set in the "value" and, if no, it will try to use old (value from request from name "some_value") and if not old then '' should be used.

    Thanks WoodyDRN for comment.

    0 讨论(0)
  • 2021-02-06 22:45

    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

    0 讨论(0)
  • 2021-02-06 22:56

    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') }}"
    
    0 讨论(0)
提交回复
热议问题