php - Laravel sort object by Key -


i have following order alphabetically key i.e first each array group "bname", followed "created_at".

{     "leads": [         {             "lead_id": 1,             "zoho_lead": null,             "bname": "abc limited",             "tname": "abc",             "source_id": 11,             "industry_id": 1,             "user_id": 1,             "created_at": "2017-09-06 15:54:21",             "updated_at": "2017-09-06 15:54:21",             "user": "sean mccabe",             "source": "unknown",             "industry": "none"         },         {             "lead_id": 2,             "zoho_lead": 51186111981,             "bname": "business name limited",             "tname": "trading name",             "source_id": 11,             "industry_id": 1,             "user_id": 1,             "created_at": "2017-06-01 12:34:56",             "updated_at": null,             "user": "john doe",             "source": "unknown",             "industry": "none"         }     ] } 

i'm trying use ksort in foreach loop:

class leadcontroller extends controller {     use helpers;      public function index(lead $leads)     {         $leads = $leads->all();          foreach($leads $key => $lead){             $lead->user = user::where('id', $lead->user_id)->first()->name;             $lead->source = source::where('id', $lead->source_id)->first()->name;             $lead->industry = industry::where('id', $lead->industry_id)->first()->name;              $lead->ksort();         }          return $leads;     } 

but following error:

call undefined method illuminate\\database\\query\\builder::ksort() 

how use function, or there laravel way of doing this, or better way altogether?

thanks.

managed return keys in alphabetical order, below solution in-case else should require it:

public function index(lead $leads) {     $leadout = array();      $leads = $leads->all();      foreach($leads $key => $lead){         $lead->user = user::where('id', $lead->user_id)->first()->name;         $lead->source = source::where('id', $lead->source_id)->first()->name;         $lead->industry = industry::where('id', $lead->industry_id)->first()->name;          //convert array         $leadorder = $lead->toarray();         //sort desired         ksort($leadorder);         //add array         $leadout[] = $leadorder;     }      return $leadout; } 

there cleaner way this, works instance, , perhaps additional answers may posted better.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -