Support the ongoing development of Laravel.io →
Database Eloquent

Hi all,

I was wondering how I could do the following. I have a user table with 2 field 'first_name' and 'last_name'.

In my view I want to show the firstname and lastname by using the implode function so I can do something like 'Frederik Jacques, Tim Cook, Barack Obama'

I can now already do this, but it shows only the firstname or last name

{{ $registration->studentGroup->students->implode('first_name', ', ') }}

So I would like to add a property to my User model class which returns the first and lastname

public function getFullName(){
    return $this->first_name . ' ' . $this->last_name
}

And then use the implode function like this

{{ $registration->studentGroup->students->implode('fullName', ', ') }} 

But this does not work. Any idea's how I can make this work?

Kind regards, Frederik

Last updated 3 years ago.
0

You can define an accessor in your model.

The doc: http://laravel.com/docs/eloquent#accessors-and-mutators

You can also add your accessor's value to the array and json representation of your model (toArray and toJson) using the $appends model variable.

The doc (see very bottom of page): http://laravel.com/docs/eloquent

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.