11

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.

3 Answers 3

25

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.

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

1 Comment

Isn't a bad practice to call directly a table by name?
4

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()) }}

2 Comments

this way you would get "Unsupported operand types"
Maybe something like this? public function scopeForSelectBox($query, $title = 'Country') { $countries = $query->lists('name', 'id')->toArray(); array_unshift($countries,$title); return $countries; }
1

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)}}

1 Comment

Laravel 5.3, the lists() method is removed in favour of the pluck() method. For Example: $clients = Client::pluck('name', 'id');

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.