Ajaxcbcb
Ajaxcbcb

Reputation: 167

Laravel 5.2 - Populate select options from database

Trying to populate the dropdownlist from database. but there having some complication. Any help? This is based on laravel framework.

Part of the code

$projects = DB::table('projects')->where('users_id','=',Auth()->User()->id)->get();

                        foreach($projects as $project){
                            echo '
                                    <option value="<?php $id = $project[id] ?>" name="parentProj">'
                                        .$projectName = $project[id].
                                    '</option>
                                  ';
                        }

Controller

public function index(){
    $projects = DB::table('projects')->lists('id','projectName');

    return view('pages.todo', ['projects'=> $projects);
}

Upvotes: 0

Views: 12539

Answers (2)

vonec
vonec

Reputation: 701

Or you can use the Forms & HTML package from Laravel Collective: https://laravelcollective.com/docs/5.2/html#drop-down-lists

{{ Form::select('Projects', $projects)) }}

Upvotes: 0

Jilson Thomas
Jilson Thomas

Reputation: 7303

In your view:

<select name="parentProj">
    @foreach($projects as $project)
     <option value="{{ $project->id }}">{{ $project->projectName}}</option>
    @endforeach
</select>

Upvotes: 3

Related Questions