Dev.W
Dev.W

Reputation: 2360

Select2 Laravel Json

I've been playing with this setup for a while now but I can't get the results to return to the select box.

See my streamlined code for question below, everything is initialized however I'm just not getting anything back.

Code: JS.

$('.js-data-example-ajax').select2({
  ajax: {
    type: 'GET',
    url: '/ajax/staff',
    delay: 250
  }
});

HTML

<select class="form-control js-data-example-ajax">
   <option value="3620194" selected="selected">select2/select2</option>
</select>

Route

Route::get('ajax/staff', 'APIController@staff');

Controller

public function staff()
    {
        $staff = staff::all();
        return $staff;
    }

Upvotes: 0

Views: 775

Answers (2)

Adnan
Adnan

Reputation: 8210

Try with this

  public function staff()
        {
            $staff = staff::all();
             echo json_encode($staff);
        }

Upvotes: 1

Alex
Alex

Reputation: 4774

Your controller isn't returning JSON.

Try it this way:

public function staff()
{
   $staff = staff::all();
   return response()->json($staff);
}

Upvotes: 0

Related Questions