Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel-4 how to populate select box from database with id value and name value

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.

like image 260
GSG Avatar asked Nov 16 '13 14:11

GSG


3 Answers

Found a way to do this

ClientController.php

public function create() {

  // queries the clients db table, orders by client_name and lists client_name and id
  $client_optons = DB::table('clients')->orderBy('client_name', 'asc')->lists('client_name','id');

    return View::make('projects.create', array('client_options' => $client_options));
}

create.blade.php

// this form is now populated with the value as id and the option names as the client_name
{{ Form::select('clients', $client_options , Input::old('clients')) }}

Hope this helps others having problems like this.

like image 107
GSG Avatar answered Oct 18 '22 21:10

GSG


I also had a think about this and came up with the following:

In my model:

public function scopeSelect($query, $title = 'Select') {
    $selectVals[''] = $title;
    $selectVals += $this->lists('name', 'id');
    return $selectVals;
}

Then I can just put this in my views:

{{ Form::select('select_id', Model::Select('Blank option'), '', array()) }}
like image 27
Qubical Avatar answered Oct 18 '22 21:10

Qubical


Here you can use lists method. In your client controller do

$clients = Client::lists('name', 'id');

And in view, just use the variable like,

{{Form::select('client', $clients)}}
like image 30
Jyothu Avatar answered Oct 18 '22 20:10

Jyothu