0

in my create function, I pass my view a list of users which I then display. However, I have changed it to the following

<select class="internalWork" name="internalWork">
    <option value=""></option>
    @foreach($users as $user)
        <option value="{{ $user->userName }}">{{ $user->userName }}</option>
    @endforeach
</select>

So I have added an empty option before the list is displayed. This is fine because this tables column allows null input.

In my edit function, I get the list

$users = User::lists('userName', 'userName');

I then pass this to my edit view. In my edit view, I have the following

<div class="form-group">
    {!! Form::label('internalWork', 'Internal work for:', array('class' => 'col-sm-5 control-label red')) !!}
    <div class="col-sm-7">
        {!! Form::select('internalWork', $users, null, ['class' => 'internalWork']) !!}
    </div>
</div>

If I selected a user in the create, then when I go to the edit page, that user is selected by default. However, if I do not select a user in the create, on the edit page, the first user in the list is displayed by default.

How can I get it to display an empty input if the database value is null?

Thanks

2 Answers 2

1

I think the issue is that although you are providing an empty option in the create view, you are not doing so in the edit view.

If, in your edit function, you populate your array like this:

$users = ['' => ''] + User::lists('userName', 'userName');

then I think the empty option will be selected if userName is empty. This is essentially concatenating an empty pair to the beginning of the $users array that you're passing to the view.

You might also consider constructing the create view in the same way - using the above code to generate your $users array in your create method and then using the Form facade in the view.

I've found this quite useful for working with Form::select(): Creating a Select Box Field

Sign up to request clarification or add additional context in comments.

Comments

1
@if (!empty($users)
// the Form
@else
// No result
@endif

OR

array_unshift($users, "There are no users") // Prepend null value to $users
...
// Form::select(..etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.