Skip to content
Advertisement

How to load data from cache to a form in a laravel application?

I’m developing a site in laravel. I have a view for editing and creating posts that brings the data stored from the database. For example:

<input class="form-control" id="title" name ="title" type="text" value="@if($article != null){{$article->title}}@endif">

The problem is that when the user makes modifications, try to save the data and has validation errors, he loses all the modifications he made (because when laravel reloads the page, the data is again loaded from the database). This is the validator code:

$validated = $request->validate([
            'title' => 'required|max:255',
            'slug'  => 'required|max:255',
            'encyclopedia_category_id' => 'required',
        ]);

Is there a way to load the data from the cache? Thanks for your help! Sorry if my english is not good, I hope you have understood me.

Advertisement

Answer

use old() global helper to repopulate the form.

<input class="form-control" id="title" name ="title" type="text" value="{{old('title',$article->title??null)}}">

Ref:https://laravel.com/docs/8.x/requests#retrieving-old-input

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement