Laravel pluck with custom method from model


Laravel pluck with custom method from model



I'm using Laravel 5.6. I have a users table and firstname and lastname fields.


5.6


users


firstname


lastname



In my User model I also have this function


User


public function name()
{
return $this->firstname . ' ' . $this->lastname;
}



And now in another controller, I want to create a dropdown menu with all the users. But I would like to display the name() and not only the first/lastname.


name()



I'm currently using this


$users = AppUser::pluck('lastname', 'id');
return view('myview', compact('MY_collection' , 'users'));



And in my view (with collective/html)


{!! Form::select('user', $users, isset($user) ? $MY_collection->user: null, ['class' => 'form-control']) !!}



Is is possible to use pluck with a method function ? Or should I do something else?



I am also aware of the accessor solution but I don't have a name attribute in the database, so it is not working.


name




3 Answers
3



Accessors can be any name you choose, they just have to start with get and end with Attribute:


get


Attribute


public function getNameAttribute() {
return $this->firstname . ' ' . $this->lastname;
}

$user->name

// this will give the result as above
public function getBlahBlahBlahAttribute() {
return $this->firstname . ' ' . $this->lastname;
}

$user->blah_blah_blah



Either of these should work with pluck.


User::get()->pluck('name');

$user->setAppends(['name']);
$user->pluck('name');





wont work on the pluck method
– SnakeFoot
Jun 29 at 10:38



You can concate the first name and last name like this.


$user = AppUser::select(DB::raw('CONCAT(firstname, ", ", lastname) AS full_name'),'id')->pluck('full_name','id');



Apart from declaring the accessor name starting with "get" and ending with Attribute(camelCase) :getFullNameAttribute===full_name in pluck.



You should add the Accessor to $appends :


protected $appends=['<your Accessor name>']
//protected $appends=['full_name']
//usage:
//$this->full_name



the $appends array gets accessors to append to the model's array form.



then use it:


collect(json_decode($modelName->tojson(),true))->pluck('full_name','id')





With your tips, it is indeed working for $user->tojson() by example. But the $user->pluck('name') still return me Column not found
– Raccoon
Jun 29 at 10:38



$user->tojson()


$user->pluck('name')


Column not found





try this aproach: laracasts.com/discuss/channels/laravel/…
– SnakeFoot
Jun 29 at 10:42






collect(json_decode($user->tojson(),true))->pluck('full_name','id')
– SnakeFoot
Jun 29 at 10:50







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV