samedi 5 décembre 2020

How to sort object in laravel

I am fetching data from database. then I am adding new value in that object. after that i am trying to sort object that value but it is not working

Here is the code

   public function search(Request $request)
    {
 

    $providers = Provider::get()->map(function($item) use($request){
        $item->setAttribute('distance',$this->addDistanceToProvider($item,$request)) ;
    
        return $item;
    })->sortBy('distance');
  
        return response()->json($providers);

    }

ajax call and then appending it here

$("#search").click(function(){
     
  $.get("/search",
  {
    longitude: $('#lat').val(),
    latitude: $('#long').val(),
    address: $('#autocomplete_search').val(),
    
  },
  function(data){
    $('#provider_area').empty();
    $.each(data, function(index, provider) {
    embedData(index,provider)
        
});
  });
});
function embedData(index,provider){
    $('#provider_area').append(`    <div class="kt-widget5__item" ><div class="kt-widget5__content">
                                    <div class="kt-widget5__pic">
                                    
                                        <img class="kt-widget7__img" src="/media/logos/logo.png" alt="">
                                    </div>
                                    <div class="kt-widget5__section">
                                        <a href="#" class="kt-widget5__title">
                                            ${provider.name}
                                        </a>
                                        <p class="kt-widget5__desc">
                                            ${provider.location}
                                        </p>
                                        <div class="kt-widget5__info">
                                            <span>Distance:</span>
                                            <span class="kt-font-info" >${provider.distance}</span>
                </div>
                                    </div>
                                </div>
                    
                                    </div>
                                    
                                </div></div>`)
}

How I can sort is by distance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire