Problems writing to an input from the controller

Asked

Viewed 24 times

1

Hello, I’m having trouble entering data from controller.

I am doing the activities of a Code House Laravel book to learn and needed to do this to update the record of a bank tuple, I ended up using the following workaround:

<input name="nome" class="form-control" value="{{$p->nome}}" />

It worked, however, in the book, the author teaches to keep the data in the form during a validation error using the following code:

<input name="nome" class="form-control" value="{{ old('nome') }}" />

This broke my scheme of assigning direct in value the attributes of the object coming from the database... Thus, I will have to add the values in the form inputs via controller during the data update.

I searched in several places, I even found some reference, but I could not use them due to the lack of namespace that should import.

From now on, thank you.

1 answer

2

To solve this problem do:

<input name="nome" class="form-control" value="{{ isset($p) ? $p->nome : old('nome') }}" />

in case it will work like this, if the object $p has set it will bring the data of the attribute name, if not it takes the die if it has the last request.

  • 1

    Thanks! I didn’t know this isset($var) method. I’m new to PHP, rs.

Browser other questions tagged

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