简体   繁体   中英

laravel ajax get request on orderBy => internal server error

i'd like to performe a simple "orderBy" data from mssql server table with an ajax request... but i get an Internal Server Error 500

heres my route:

Route::get('intranet_admin/gestioneStatoUtenti/rotte', 'UserController@orderByDrop');

here's my controller:

public function orderByDrop(){
    $selectedValue = Input::get('filter_id');

        $fetchFilter = User::orderBy($selectedValue)->get();

        return Response::json($fetchFilter);
}

here's my view:

 <div class="large-3 columns">

        <select name="filter_id" id="filterValue">
              <option value="TLK">Tlk</option>
              <option value="StatoUtent">Stato Utente</option>
              <option value="CodFisc">Codice Fiscale</option>
              <option value="IdUtente">Id Utente</option>

        </select>


    </div>

    <script>
    $(document).ready(function($){

    $('#filterValue').change(function(e){
            console.log(e);
            var filter_id = e.target.value;

            $.get('gestioneStatoUtenti/rotte?filter_id='+ filter_id, function(data){

                        console.log(data);

            });
    });

     });
    </script>

cheers

The problem is that Response::json() expects an array, however you are passing an object.

In Laravel Model::orderBy()->get() will return an Eloquent Collection Object which has array like behaviour. To resolve this you just need to tell the collection to return itself as an array and not a collection object.

$fetchFilter = User::orderBy($selectedValue)->get()->toArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM