Fetch Dropdown list from database in L 5.2
Hello,
I want to display dropdown list items from the database, so for that I am using the following code.
Controller:
public function editProfile($id)
{
$surnames = DB::table('families')->lists('surname');
return view('editprofile', ['surnames' => $surnames]);
}
View:
<label for="lastname">Last Name</label>
<select class="form-control" name="lastname" id="lastname" data-parsley-required="true">
@foreach ($surnames as $sn)
{
<option value="{{ $sn->surname }}">{{ $sn->surname }}</option>
}
@endforeach
</select>
But I am getting this error: http://www.awesomescreenshot.com/image/1243033/3f24702f5d9c1420fcae3cac392efc71
PS: I am using Auth to check whether use is logged in or not.
Lists returns an array of all the surnames.
You code should be something like:
<option value="{{ $sn }}">{{ $sn }}</option>
Take a look at Retrieving A List Of Column Values in the Query Builder docs.
Note: lists() is deprecated since Laravel 5.2. Use pluck() instead.
@TheNodi Thank you so much for the quick help. That solved my issue.
hi @jeetdholakia may i know what routes you are using?your help will be appreciated....thank you!!!
Please or to participate in this conversation.