php - list more than one field in lists laravel method -


i using zizaco/entrust laravel package , suppose want fetch users teacher role.

i want return selected users , name , family combination :

[     1   =>  'ali nasiri',     2   =>  'majid basirati' ] 

for that, wrote code :

$teachers =     role::where('name', 'teacher')->first()->users()->lists('name', 'users.user_id'); 

but returns :

{     "1": "ali",     "2": "majid" } 

means name field returned because can define 1 field in first parameter of lists method.

how can , best solution ?

in user model, add custom attribute(accessor):

<?php  use zizaco\entrust\traits\entrustusertrait;  class user extends eloquent {     use entrustusertrait; // add trait user model      ...     protected $appends = ['full_name'];      //setter of full name attribute     public function getfullnameattribute()     {         return $this->name.' '.$this->surname;     } } 

then use access full name:

$teachers = user::wherehas('roles',function($q){     return $q->where('name','teacher'); }) ->get() ->lists('full_name'); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -