Laravel-5 How to Populate Select Box from Database With Id Value and Name Value

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

Laravel provides a Query Builder with lists() function

In your case, you can replace your code

$items = Items::all(['id', 'name']);

with

$items = Items::lists('name', 'id');

Also, you can chain it with other Query Builder as well.

$items = Items::where('active', true)->orderBy('name')->lists('name', 'id');

source: http://laravel.com/docs/5.0/queries#selects


Update for Laravel 5.2

Thank you very much @jarry. As you mentioned, the function for Laravel 5.2 should be

$items = Items::pluck('name', 'id');

or

$items = Items::where('active', true)->orderBy('name')->pluck('name', 'id');

ref: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0 -- look at Deprecations lists

Laravel 5.2 - Populate select options from database

In your view:

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

How to show selected value from database in dropdown using Laravel?

This is a example of how I do this:

<select class="js-states browser-default select2" name="shopping_id" required id="shopping_id">
<option value="option_select" disabled selected>Shoppings</option>
@foreach($shoppings as $shopping)
<option value="{{ $shopping->id }}" {{$company->shopping_id == $shopping->id ? 'selected' : ''}}>{{ $shopping->fantasyname}}</option>
@endforeach
</select>

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

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.

how populate select box with database in Laravel 5.4?

The problem is you are looping through the categories of that product, not all categories. Here is a solution:

public function edit($id)
{
$produtos = Produtos::find($id);
$categories = Category::all();

return view('functions.edit', compact('produtos', 'categories'));
}

then in your view:

<select class="selectpicker" data-live-search="true">
@foreach($categories as $categoria)
<option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
@endforeach
</select>

Laravel 5 - Pre-populate a HTML select using database value and old

Lets imagine you have sent the category value as $category.

So in every <option> tag,

<option value="animal" 
{{ old('category') == 'animal' ? ' selected' :
$category == 'anumal' ? ' selected' : '' }}>
Animal
</option>

This is what is going on there:

if there is an old value and its matching, select this,
else if the original value is matching select this,
else do nothing.

Laravel how to populate select box from database

Your mistake is that your passing one variable name to your view and trying to read with another name. I made some quick example for you. Here my table is "Pais" ("Country" in portuguese), and use an clorure route for the callback function. (Lavarel 5.4)

Route (web.php):

Route::get('/test', function () {
$countries = \App\Pais::all();
return view('test_stackoverflow')->with(['countries' => $countries]);
});

View (test_stachoverflow.blade.php):

<select name="countries" id="countries">
@foreach($countries as $country )
<option value="{{ $country->id }}">{{ $country->name }}</option>
@endforeach
</select>

Result


Another option, if you want to stay with the pluck method:

Route (web.php):

Route::get('/test', function () {
/* $countries = \App\Pais::pluck('name','id'); */
// or
$countries = DB::table('pais')->pluck('name','id');
return view('test_stackoverflow')->with(['countries' => $countries]);
});

View (test_stachoverflow.blade.php):

  <select name="countries" id="countries">
@foreach($countries as $id => $country )
<option value="{{ $id }}">{{ $country }}</option>
@endforeach
</select>

Both solutions have the same result in the generated HTML.

Hope this can help you!
Best regards.

How to populate dropdown from database laravel 5.x

Try this

In your controller

$regs = Model::pluck('name','id');

Keep your view same

Hope this will work



Related Topics



Leave a reply



Submit