Tuesday, May 23, 2017

laravel 5.4 form dropdown from database

I want to fetch drop down option value & name from database, to do that my controller code is

 $roles = Role::pluck('role','user_id');
 return view('users.add_role',compact('roles'));

to fetch this data from drop down list my view file code is

<select name="role" class="form-control" >
          @foreach($roles as $role)
            <option value=" ">  </option>
          @endforeach
</select>

but it says error

Trying to get property of non-object (View: C:\xampp\htdocs\auth2\resources\views\users\add_role.blade.php)

if i just only use

<select name="role" class="form-control" >
      @foreach($roles as $role)
        <option value=" ">  </option>
      @endforeach
</select>

then it shows role column of the table, but it not pass option value to database. so what is the correct way to generate option value & name from database?



via Masum

Advertisement