Laravel 5 - Populate select box from database example


we always require to generate dynamic select box from database table like if you have users table and you require to generate drop down box on all users from database. So in this article i will let you know how to populate drop down list from database.

In this example, i simply created users table and added some dummy data. Then i will populate select box using laravel model. So let's see bellow example:

Add Route

Route::get('myform', 'HomeController@myform');

Add Controller Method

public function myform()

{

$users = User::pluck('name', 'id');

return view('myForm',compact('users'));

}

Add View

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5 - Populate select box from database example</title>

</head>

<body>


{!! Form::open() !!}


{!! Form::select('user_id', $users, null, ['class' => 'form-control']) !!}


{!! Form::close() !!}


</body>

</html>

Ok, now you can use in your laravel app and check...Enjoy....


June 25, 2017 | 1210 Viewer | Category : Laravel

Related Posts: