I want to populate a select box with clients from my database in the projects controller as a project will belong to a client but it also belongs to the user who is logged in.
I want to create a select box like the one below:
<select>
<option value="$client->client_id">$client->client_name</option>
<option value="$client->client_id">$client->client_name</option>
</select>
I have this in laravel which populates my select field with the client names however the value attribute has the client name and I would rather this had the client_id.
The way I have done this is as below:
ProjectController.php
public function create()
{
//find logged in users clients
$clients = Auth::user()->clients;
$client_selector = array();
foreach($clients as $client) {
$client_selector[$client->client_name] = $client->client_name;
}
// load the create form (app/views/projects/create.blade.php)
return View::make('projects.create', array('client_selector' => $client_selector));
}
create.blade.php
{{ Form::open(array('action' => 'ProjectController@store', 'id' => 'createproject')) }}
<div class="form-group">
@if(count($client_selector)>0)
{{ Form::label('select_client', 'Select Client', array('class' => 'awesome')); }}
<!-- SELECT IS CREATED HERE -->
{{Form::select('client', $client_selector, array_values($client_selector)[0])}}
@endif
</div>
<div class="form-group">
{{ Form::label('project_name', 'Project Name') }}
{{ Form::text('project_name', Input::old('project_name'), array('class' => 'form-control')) }}
</div>
As you can see from the way the select is being created it is using the client_name to populate the values attributes and I'm not really much of an expert on Laravel so I'm not sure how to change these attributes.
If any could perhaps show me how this is done or has a better method of achieving this then please do give me some examples!
Thanks in advance.