What is the best practice to show old value when editing a form in Laravel?

前端 未结 6 1074
暗喜
暗喜 2021-02-01 03:04

I am writing the logic for an edit form and have some complications when displaying data in the inputs.

When I initially show the form, I show the records values like:

6条回答
  •  滥情空心
    2021-02-01 03:45

    I know this has already been answered but I thought I would leave a little snippet here for others in the future.

    Setting old value on input as @ikurcubic posted can be used the same way on radio button or select:

    
    

    Select option:

    
    

    Radio Button:

    default-value)== "M" ? 'checked' : '' }} />
    

    Another way of doing it; write a small if statement to determine which value should be evaluated:

    @php                                  
    if(old('name') !== null){
        $option = old('name'); 
    }
    else{ $option = $database->value; }
    @endphp
    
    
    
    
    
    
    
    

    Setting old value of input with an array name e.g name="name[]":

    
    

    This will give you the old value of the input with an index of 0.

    I have tested this and it works.

提交回复
热议问题