I have a model called customers
which has a custom attribute called name
; this can either by a customers full name or the company name depending on what their account type is.
class Customer extends Model
{
const BUSINESS = "Business";
const INDIVIDUAL = "Individual";
protected $table = 'users';
protected $appends = ['name'];
private $name;
public static function boot()
{
parent::boot();
static::created(function () {
});
static::updating(function () {
});
}
/**
* Get the display name for the customer
*
* if Customer::BUSINESS then use company field
* else if Customer::INDIVIDUAL then use their name
*
* @return mixed|string
*/
function getDisplayName() {
return ($this->account == Customer::BUSINESS) ? $this->company : $this->getContactName();
}
public function getContactName()
{
return ucfirst($this->first_name) . " " . ucfirst($this->last_name);
}
/**
* @return mixed
*/
public function getName()
{
return $this->getDisplayName();
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
}
I want to be able to ORDER BY this custom attribute if possible.
At the moment Laravel throws an error saying name
is not a defined column.
Is there a way to do this with Laravel Query builder?