5

I am currently rendering and populating a form in laravel using the laravelcollective plugin. this is working as expected:

{!! Form::model($user, ['action' => 'user@updateUser']) !!}

<div class="form-group">
    {!! Form::label('user_name', 'Name') !!}
    {!! Form::text('user_name') !!}
</div>

<button class="btn btn-success" type="submit">Update</button>

{!! Form::close() !!}

The above code generates the form and populates the input field with the user's name.

If I want to add a class attribute to the form input like so:

{!! Form::text('user_name', '', ['class' => 'form-control']) !!}

It does not populate the input value because in theory I've set the default value (second parameter) to ''.

Is there a way of populating the value and adding a class without explicitly doing so like this:

{!! Form::text('user_name', $user->user_name, ['class' => 'form-control']) !!}

By doing the above it defeats the object of rendering the form via a model {!! Form::model($user, ['action' => 'user@updateUser']) !!} as I may as well parse the $user as a variable onto the template, which I don't want to do.

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
steve
  • 471
  • 6
  • 15

1 Answers1

1

Then you can use null instead "" blank

{!! Form::text('user_name', null , ['class' => 'form-control']) !!}

You can also use old function

{!! Form::text('user_name', $user->user_name or old('user_name'), ['class' => 'form-control']) !!}

This $user->user_name or old('user_name') can be expressed as

If $user->user_name is exist then put value of $user->user_name otherwise check for old input which is "" blank first time and have some value if you redirect back if any error comes.

or simply use isset method or in php 7 use $user->user_name ?? ''

{!! Form::text('user_name', $user->user_name ?? '', ['class' => 'form-control']) !!}
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • Also can be written as `old('user_name', $user->user_name)`. – Lovepreet Singh Jun 08 '18 at 12:04
  • @LovepreetSingh : No it will not work as you suggest, second param of `old` is for default value and if first time you come there will be no value and you will get error. – Niklesh Raut Jun 08 '18 at 12:10
  • 1
    Can write as: `old('user_name', empty($user->user_name) ? '' : $user->user_name)` – Lovepreet Singh Jun 08 '18 at 12:11
  • Yes now it will work :), another alternative is `old('user_name', $user->user_name ?? '')` – Niklesh Raut Jun 08 '18 at 12:13
  • Thanks, but this doesn't resolve the issue as I still need to explicitly call `$user->user_name` as opposed to letting the `Form::` method populate it. – steve Jun 08 '18 at 12:27
  • @steve that's not strictly true, this answer is giving you full options. You could keep it simple, without calling the variable like so `{!! Form::text('user_name', old('user_name'), ['class' => 'form-control']) !!}` this would also aid in form validation too. – Kevin Lynch Jun 08 '18 at 12:37
  • @steve : use `null` as `{!! Form::text('user_name', null , ['class' => 'form-control']) !!}` – Niklesh Raut Jun 08 '18 at 12:42
  • @C2486 `null` works, thanks. Could you put that as the main answer and i'll mark it as correct. – steve Jun 08 '18 at 12:52