0

In Laravel drop downs is there a way to have multiple values in the drop down value?

Normally you would write a command like this to get a name and your id for the select box.

Project::where('project_id', '=', Session::get('project_id'))->lists('project_name', 'id');

and it will make a drop down like this

<select>
<option value="13">12345</option>
<option value="16">14-100</option>
<option value="17">14-200</option>
<option value="31">987</option>
</select>

I want to know if I can some how have it display another value from the db in value like created_by, so it would look something like this.

<select class="form-control" name="project">
<option value="13">12345 Billy user</option>
<option value="16">14-100 Sally user</option>
<option value="17">14-200 Thomas user</option>
<option value="31">987 Lola user</option>
</select>

Is something like this possible?

3 Answers 3

2

You could get the fields you need in the controller:

$projects = Project::where('project_id', '=', Session::get('project_id'))->get('id', 'project_name', 'created_by');

Then pass it to the view:

return View::make('my.view', array('projects' => $projects));

There you create the option tags manually:

<select class="form-control" name="project">
@foreach($projects as $project)
    <option value="{{ $project->id }}">{{ $project->project_name }} {{ $project->created_by }}</option>
@endforeach
</select>
Sign up to request clarification or add additional context in comments.

Comments

2

There are a couple options here.

You can do it directly in the SQL. This will keep everything server side and will not take up any memory or time building models.

Project::where('project_id', '=', Session::get('project_id'))
    ->lists(DB::raw('CONCAT(project_name, " ", created_by)'), 'id');

However, if your logic is a little more complicated, or you're looking for a more "Laravel-ish" solution, you can create an accessor method on your Project model. With this method, you end up creating all the models before calling the lists method:

// model
class Project extends Eloquent {
    // accessor method to provide 'name_by' attribute
    public function getNameByAttribute() {
        return $this->project_name . ' ' . $this->created_by;
    }
}

// if you limit the select, make sure you at least select the fields you need
$projects = Project::select('id', 'project_name', 'created_by')
    ->where('project_id', '=', Session::get('project_id'))
    ->get();
// call lists on the Collection of models, which will allow use of the accessor
$projectList = $projects->lists('name_by', 'id');

Docs on Laravel accessors are here, if you're interested.

Comments

-1
public static function tableListToDropDown($tableName='',$nameField='',$idField='')
    {
        if ($idField == null)
        {
            $idField = "id";    
        }

        $listFiledValues = DB::table($tableName)->select($idField,$nameField)->get();
        $selectArray=[];
        foreach ($listFiledValues as $listFiledValue)
        {
            $selectArray[$listFiledValue->$idField] = $listFiledValue->$nameField;
        }

        return $selectArray;
    }

3 Comments

it would provide more value, if you would explain your uncommented code
This can be used in form builder
Please do not add such explanation to the comment section, rather edit your answer to contain all relevant information

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.