In the case of getFullNameAttribute, both full_name and fullName work?
Jun 8, 2017
6
Level 9
Mixed model relationship/attribute casing (camelCase vs snake_case) is bugging me
When using camelCase for model methods and having a multi-word relationship method names it bugs me that I have mixed casing between relationships and attributes.
For example, given the following model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function creditCard()
{
return $this->hasOne(CreditCard::class);
}
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
}
When accessing the various relationships/attributes of the model I get mixed casing like this:
<?php
// Get the user's full name
$user->full_name;
// Get the user's credit card info
$user->creditCard->number;
$user->creditCard->expiration_date; // This is especially annoying
I would rather all the properties have matched casing like so:
<?php
$user->credit_card->number;
$user->credit_card->expiration_date; // Especially here
The obvious solution is to just use snake_case for the relationship method names but then my model method casing will be mixed. Is there a simple/proper solution to this problem?
Please or to participate in this conversation.