katie hudson
katie hudson

Reputation: 2893

Laravel 5 defining old input on select

in my controller, I am passing a list of clients to the view

public function edit(Project $project)
{
    $clients = Client::select('clientName', 'id')->get();

    return View::make('projects.edit', compact('project', 'clients'));
}

Now in my view, I am currently doing this

<div class="form-group">
    {!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        <select class="clientName" name="clientName">
            @foreach($clients as $client)
                @if (Input::old('clients') == $client->id)
                <option value="{{ $client->id }}" selected="selected">{{ $client->clientName }}</option>
                @else
                <option value="{{ $client->id }}">{{ $client->clientName }}</option>
                @endif
            @endforeach
        </select>
    </div>
</div>

What I am trying to do is have the default select option set as the old input. At the moment, the select displays with all the clients, but the old value is not default.

How would I go about making it the default option?

Thanks

Update I do a alternative way I am trying. In my edit function I do

public function edit(Project $project)
{
    $clients = Client::lists('clientName', 'id');

    return View::make('projects.edit', compact('project', 'clients'));
}

And then in my view I do

<div class="form-group">
    {!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::select('clientName', $clients, Input::old('clients'), ['class' => 'clientName']) !!}
    </div>
</div>

Seem to have the same issue though, the client is not the old client as the default selected option.

Thanks

Upvotes: 0

Views: 2226

Answers (1)

user2094178
user2094178

Reputation: 9464

Your select name is clientName but your old input is looking to a field with the name clients.

The following should work:

<option value="{{ $client->id }}" {!! old('clientName', $project->client->id) == $client->id ? 'selected="selected"' : '' !!}>{{ $client->clientName }}</option>

Upvotes: 2

Related Questions