Search code examples
phplaravelformsreturnsubmit

Return with last id laravel


I have a form, with two submit buttons first button is submit and return to a page, and the second button is to submit and return to the same form, what I want to do is when you stay at the same form that 2 fields remain filled with the data that has been submitted, but I tried different ways and it didn't work. This is my controller

$retour->ordernumber =  request('ordernumber');
$retour->customername = request('customername');

$retour->save();

        if ($request->submit === 'submit') {
            return redirect('/return')->with('message', 'Approved');

        } else {
            return redirect('/return/create')->with('message', 'Approved, you can make a new report.');
        }

And the fields I want remain filled are the ordernumber and customername

<div class="form-group">
<label>Ordernumber</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">653</span>
</div>
<input type="text" name="ordernumber" class="form-control" placeholder="Enter ordernumber" value="{{ old('ordernumber') }}">
</div>
</div>
<div class="form-group">
<label>Customername</label>
<input type="text" name="customername" class="form-control" placeholder="Enter customername" value="{{ old('customername') }}">
</div>

Solution

  • you can do this by using session.

    in controller

    use Session;               // at the top
    $retour->ordernumber =  request('ordernumber');
    $retour->customername = request('customername');
    $retour->save();
    
    Session::put('ordernumber',request('ordernumber'));
    Session::put('customername',request('customername'));
    Session::save();
    

    in view

    <div class="form-group">
       <label>Ordernumber</label>
       <div class="input-group">
          <div class="input-group-prepend">
             <span class="input-group-text">653</span>
          </div>
          <input type="text" name="ordernumber" class="form-control" placeholder="Enter ordernumber" value="{{ Session::get('ordernumber') }}">
       </div>
    </div>
    <div class="form-group">
       <label>Customername</label>
       <input type="text" name="customername" class="form-control" placeholder="Enter customername" value="{{ Session::get('customername') }}">
    </div>
    
    

    If you want to use old() helper then you need to rediect with ->withInput();

    return redirect('/return')->with('message', 'Approved')->withInput();