I have a following query to select a client with its title ('Dr.' , 'Prof.', etc).
I have a title_idcolumn and all the possible titles in an array under config/titles.php
Titles.php
<?php
return [
'titles' => [
'1' => 'Dr.',
'2' => 'Prof.',
]
];
In my query I want to achieve that
IF title_id = 1 then add an attribute as title = 'Dr.'
If it is 2 then 'Prof.' and so on.
I want to do it in the query instead looping outside the results.
Client::select('clients.id as client_id', 'clients.firstname', 'clients.lastname')
->when('clients.title_id' == 1, function ($query) {
return $query->select('clients.title_id as title = `Dr.`'); })
->when('clients.title_id' == 2, function ($query) {
return $query->select('clients.title_id as title = `Prof.`'); })
->first();
However, it ignores the title_id. It does not give back error. It just ignores the title_id
What am I doing wrong?