katie hudson
katie hudson

Reputation: 2893

laravel populate select box

I have seen similar questions asked before, but was unable to find a solution to my problem. So I have a ClientController and within it this function

public function edit(Client $client)
{
    return view('clients.edit', compact('client'));
}

So that passes the client object to my edit view. This view is pretty much the following

{!! Form::model($client, ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
@include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}

So it is using a partial for the form. At the moment, the partial looks like so

<div class="form-group">
    {!! Form::label('clientName', 'Client Name:') !!}
    {!! Form::text('clientName') !!}
</div>
<div class="form-group">
    {!! Form::label('clientStatus', 'Client Status:') !!}
    {!! Form::select('clientStatus') !!}
</div>

When I visit the edit page for a client, I can see the form. The clientName is populated with the clientName value. The clientStatus is populated if I put it as a text input, but I cant get it to populate within a select as shown above. Furthermore, clientStatus can either be New or Existing. I need the select box to be pre-populated with the status of the client that is being edited, but I need the other option available within the select as well. So if the clientStatus is New, New should be pre-selected within the select box and if I open the select, Existing should be the other option.

What would be the best way to achieve this?

Thanks

Upvotes: 1

Views: 173

Answers (1)

AbstractChaos
AbstractChaos

Reputation: 4211

Modify your select to include an array of the possible values.

Basic Select - Label is value

{!! Form::select('clientStatus',['New','Existing']) !!}

Key Value Select - Key is value

{!! Form::select('clientStatus',[ 1 => 'New', 2 => 'Existing']) !!}

Form model will then set the value in the select to the one in the model.

More information see the docs.

Upvotes: 1

Related Questions