Problems writing to an input from the controller

1

Hello, I'm having trouble inserting the data from the controller.

I am doing the activities of a House of Code Laravel book to learn and I needed to do this to update the registry of a tuple from the bank, 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 you 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 has broken my schema of assigning directly the value of the attributes of the object coming from the database ... So I will have to add the values in the form inputs via controller during the data update.

I searched in several places, until I found some reference, but I could not use them because of the lack of the namespace I should import.

Thanks in advance.

    
asked by anonymous 06.09.2016 / 18:31

1 answer

2

To resolve this issue, do the following:

<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 been set it will bring the data of the attribute name, otherwise it will get the data if it has the last request .

    
06.09.2016 / 18:33