Level 51
Why not handle this outside of the query?
Something like:
$client = Client::findOrFail($request->id);
$clientName = "{$client->last_name}, {$client->first_name}";
if (!is_null($client->spouse_first_name))
$clientName .= " & {$client->spouse_first_name}";
return response($clientName);
Or even better, add some model methods so this is always available:
Client.php
public function fullName() {
return "{$this->last_name}, {$this->first_name}";
}
public function fullNameWithSpouse()
{
if (!is_null($this->spouse_first_name))
return $this->fullName() . " & " . $this->spouse_first_name;
return $this->fullName(); // if no spouse
}
so you could simplify your original query with:
$client = Client::findOrFail($request->id);
return repsonse($client->fullNameWithSpouse());