mardi 2 janvier 2018

Laravel 5.1 How to sort collection using lists() method

I have two questions/help to fix here:

  1. How to reduce redundancy in this line of code $this->all()->lists('name', 'id')->all()
  2. How to call mutator by using orderBy method.

helper.php

if (! function_exists('withEmpty')) {

    function withEmpty($selectList, $emptyLabel = '')
    {
        return array('' => $emptyLabel) + $selectList;
    }
}

Agent.php (model)

public function getNameAttribute($value){
    return $this->lname.', '.$this->fname.' '.$this->mname;
  }

public function listAgents($emptyLabel = '--Select Agent--'){

    return withEmpty($this->all()->lists('name', 'id')->all(), $emptyLabel);


  }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire