Get users that are presidents and compute their name I have a user table like this

id | firstname | surname  | title 
1  | Bob       | Batman   | Dr.
2  | Alice     | Grimley  | Prof.
3  | Clara     | Caprio   |


In the user model class I have a function that computes the name

class User extends Authenticatable
{
    //...

    public function computeName()
    {
      return  trim($this-&gt;title . ' ' . $this-&gt;firstname . ' ' . $this-&gt;surname);
    }

    //...
}


In addition I have a 1:n relation with a table sortUser2Pres where n can only be 0 or 1. The relation maps if a user is a president or vice-president in a country.

user_id | country | type 
1       | DE      | vice-president
2       | DE      | president


Now I wish to compute a name list of all users who are president. I can achieve it as follows:

$presidents = DB::table('user')
               -&gt;select('firstname','surname','title')
               -&gt;join('sortUser2Pres s', 's.user_id','=','user.id')
               -&gt;where('s.type','president')
               -&gt;get();

$names =  [];
foreach ($presidents as $user) {
    $names[] = trim($-&gt;title . ' ' . $-&gt;firstname . ' ' . $user-&gt;surname);
}


However, I think its bad that I cannot make use of the functions from the user model like computeName and that I have to copy &amp; paste this function. Isn't there a cleaner way of achieving such a name list in Laravel and making use of the existing functions in the user model?
why do you have a 1:n relation? you can just add a 1:1 or column to your user table which has a type column for example and it holds president or vice-president values and then you can have a function in your model to check if it's a president or vice-president.

You can add a function to your repository to get all user which they are president with a simple query.