Keep data already filled in the form

Asked

Viewed 199 times

0

When bringing the form for the user to fill, I need that, if the data already exists in the database, it brings me the form filled with the possibility of the user "edit", but always creating a new record and not changing the existing.

Note: Not the same as using {{old('campo')}} because it’s only for one request error. Thanks for your help.

<label for="peso">Peso (Kg) : </label>
<input type="text" name="peso" class="form-control" v-model="peso"/>
<label for="altura">Altura (m) : </label>
<input type="text" name="altura" class="form-control" v-model="altura" v-mask="'#.##'"/>
<label for="peso">Idade: </label>
<input type="text" name="idade" class="form-control" v-model="idade"/>
<br/>
  • If it will be a new record, there is no id associate. If there is no id associated, how will we know if the information is already in the database?

  • Weight, height and age information are associated with the user, by user id I would see if there is already data in the database.

1 answer

1


There is the method replicate Model that can help you, it creates a clone of the non-existent model in the database. (link)

Let’s say your model is Informacoes, in the controller you can check if data already exists in the database and then you replicate the latest information.

//Exemplo 
public function metodoDoController() {
    $informacao = $usuario->ultimaInformacao();
    $informacao = $informacao->replicate();
    return view('SUA_VIEW', compact('informacao'));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.