Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeetdholakia's avatar

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.

0 likes
3 replies
TheNodi's avatar
TheNodi
Best Answer
Level 11

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.

altf's avatar

hi @jeetdholakia may i know what routes you are using?your help will be appreciated....thank you!!!

Please or to participate in this conversation.