Search code examples
inputcoding-stylelaravelissetlaravel-3

Populating inputs with table values in laravel


I have a profile edit page that has multiple fields like the one below. These are being populated by my table values for a user address if the user has an address.

It all works fine even if I only have one table value for any of these inputs because the $address object will atleast be set.

{{ Form::label('firstname', 'First Name') }}
{{ Form::text('firstname', $address->firstname, array('placeholder' => 'Please provide a first name')) }}

When I have absolute no $address values(absolutely no row in the address table for that user) I'm obviously getting an error of:

Trying to get property of non-object

How do I fix this in a clean matter? I know using something like isset works but it would clutter all my inputs fields up by going:

{{ Form::text('firstname', @if(isset($address))$address->firstname @endif, array('placeholder' => 'Please provide a first name')) }}

(Don't know if this is the correct syntax, but you get the idea.)

I can't just throw an isset around the label and input because I still need user to be able to set them even if there isn't an address at all.


Solution

  • In your controller, if the user has no address you could assign a new (empty) address and pass it in to your view, this way the form won't fail, and it won't need extra logic within your template.