Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sunrise's avatar

Old data can not display in "edit page"

Old data can not display in "edit page".

view

<div class="card card-block">
    <h4 class="card-title">edit</h4>
    {!! Form::model($person, array('url' => '/person/'.$person->id, 'method' => 'patch', 'class' => 'form-horizontal', 'files'=> true)) !!}
        {!! csrf_field() !!}

        <fieldset class="row form-group{{ $errors->has('name') ? ' has-error' : '' }}">
            <label class="col-xs-2 control-label">name:</label>
            <div class="col-xs-10">
                <input type="text" class="form-control" name="name"
                       value="{{ old('name') }}">
                @if ($errors->has('name'))
                    <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                @endif
            </div>
        </fieldset>

        <fieldset class="row form-group{{ $errors->has('email') ? ' has-error' : '' }}">
            <label class="col-xs-2 control-label">email:</label>

            <div class="col-xs-10">
                <input type="text" class="form-control" name="email" value="{{ old('email') }}">
                @if ($errors->has('email'))
                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                @endif
            </div>
        </fieldset>

        <fieldset class="row form-group{{ $errors->has('aplication_way') ? ' has-error' : '' }}">
            <label class="col-xs-2 control-label">aplication way:</label>
            <div class="col-xs-10">
                <div class="c-inputs-stacked">
                    <label class="c-input c-radio">
                        <input id="radioStacked1" name="aplication_way" type="radio">
                        <span class="c-indicator"></span>
                        telephone
                    </label>
                    <label class="c-input c-radio">
                        <input id="radioStacked2" name="aplication_way" type="radio">
                        <span class="c-indicator"></span>
                        email
                    </label>
                </div>
                @if ($errors->has('aplication_way'))
                    <span class="help-block">
                                        <strong>{{ $errors->first('aplication_way') }}</strong>
                                    </span>
                @endif
            </div>
        </fieldset>

        <fieldset class="row form-group{{ $errors->has('image') ? ' has-error' : '' }}">
            <label class="col-xs-2 control-label" for="inputFile1">image:</label>
            <div class="col-xs-10">
                <input type="file" class="form-control-file" id="inputFile1" name="photo"
                       value="{{ old('image') }}">
                @if ($errors->has('image'))
                    <span class="help-block">
                                        <strong>{{ $errors->first('image') }}</strong>
                                    </span>
                @endif
            </div>
        </fieldset>

        <fieldset class="form-group">
            <div class="col-xs-12">
                <button type="submit" class="btn btn-primary">
                    update
                </button>
            </div>
        </fieldset>
    {!! Form::close() !!}
</div>

controller

    public function edit($id)
    {
        $person = Person::findOrFail($id);
        return view('person.edit', compact('person'));
    }
0 likes
12 replies
Snapey's avatar
Snapey
Best Answer
Level 122

'Old data' is a term related to validation errors. You want to show the values from the database?

the second parameter to the old() function is the original value;

    value="{{ old('name', $person->name)}}">

5 likes
Snapey's avatar

Yes, a bit trickier.

In the case of radio and checkboxes you have to use the old(x,y) function as part of a test to know if the checkbox should be checked or the radio set active.

Checkboxes;

  <input type="checkbox" name="bike" value="Bike" 
    @if(old('bike',$transport->bike))
        checked
    @endif
    > I have a bike<br>

  <input type="checkbox" name="car" value="Car" 
    @if(old('car',$transport->car))
        checked
    @endif
    > I have a car<br>

Same with radio, but you have to compare the value with the radios to see which radio is selected

  <input type="radio" name="gender" value="male"  @if(old('gender',$profile->gender)=='male') checked @endif > Male<br>
  <input type="radio" name="gender" value="female"@if(old('gender',$profile->gender)=='female') checked @endif > Female<br>
  <input type="radio" name="gender" value="other" <?php old('gender',$profile->gender)=='other')?'checked':''; ?> > Other

in the final row, I show it using plain old php instead of blade syntax

2 likes
Snapey's avatar

Selects are more tricky because you have to test the old value against each of the options.

<select name="cars">

    @foreach($carTypesList as $type =>$name)

          <option value="{{ $type }}"
            @if(old('cars',$profile->car) == {{ $type }})
                selected
            @endif 
                >{{ $name }}
        </option>
        
    @endforeach 

</select>

if you don't like mixing in the test in with the html so much, you could duplicate the line like;

<select name="cars">

    @foreach($carTypesList as $type =>$name)

        @if(old('cars',$profile->car) == $type )
             <option value="{{ $type }}" selected >{{ $name }}</option>
        @else
             <option value="{{ $type }}">{{ $name }}</option>
        @endif
        
    @endforeach 

</select>

Whilst this is not 'dry' it's easier to read and therefore quicker to work with.

2 likes
sunrise's avatar

@Snapey How to show the original values of textarea, and input type="file"?

Snapey's avatar

No idea for file I'm afraid since the server will not remember the file that you selected.

Textarea is very straightforward;

        <textarea name="notes" rows=5 >{{ old('notes',$profile->notes) }}</textarea>

ps. I fixed a couple typos in the earlier posts

sunrise's avatar

@Snapey
Radio: if set rule 'application_way'=>'required',it can not be submitted,if delete this rule ,database can not get checked radio value.

view

<fieldset class="row form-group{{ $errors->has('aplication_way') ? ' has-error' : '' }}">
    <label class="col-xs-2 control-label">aplication way:</label>
    <div class="col-xs-10">
        <div class="c-inputs-stacked">
            <label class="c-input c-radio">
                <input id="radioStacked1" name="aplication_way" type="radio" value="1"
                       @if(old('aplication_way',$person->aplication_way)=='1') checked @endif>
                <span class="c-indicator"></span>
                telephone
            </label>
            <label class="c-input c-radio">
                <input id="radioStacked2" name="aplication_way" type="radio" value="2"
                       @if(old('aplication_way',$person->aplication_way)=='2') checked @endif>
                <span class="c-indicator"></span>
                email
            </label>
        </div>
        @if ($errors->has('aplication_way'))
                                <span class="help-block">
                                        <strong>{{ $errors->first('aplication_way') }}</strong>
                                    </span>
        @endif
    </div>
</fieldset>

controller

    public function store(Requests\StorePersonRequest $request)
    {
        $person = new Person($request->except('photo'));
        $person -> user_id = \Auth::id();

        $file = $request->file('photo');
        $destinationPath = 'uploads/';
        $extension = $file->getClientOriginalExtension();
        $fileName = \Auth::user()->id . '_' . time() . '.' . $extension;
        $file->move($destinationPath, $fileName);
        $person -> image = '/'.$destinationPath.$fileName;

        $person->save();

        return redirect()->action('PersonController@show', ['id' => $person->id]);
    }

StorePersonRequest

    public function rules()
    {
        return [
            'name'=>'required',
            'email'=>'required',
            'profile'=>'required',
            'application_way'=>'required',
            'image'=>'image'
        ];
    }

Snapey's avatar

well, I don't know if its this, but in your rules you have application with two 'p' but in the form its only one.

1 like
bincy's avatar

Hi, I have a form, say for eg. a student edit form inside which contains another form for adding their work experience. After I entered all details like name and all, I clicked on the link to edit my work experience. By the time I return from that form after updating work experience, I lost all that data I entered before (username, email etc.). How can I preserve my data in the first form? I know that's because of page reloading but how can I do this?

Snapey's avatar

@bincy the link to the work experience needs to turn into a form submit button. Then before you show the new form, save all the values of the first.

Then when returning, pull back the data and repopulate the first form.

Please or to participate in this conversation.