Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bart's avatar
Level 13

Column alias definitions

Hey everybody,

I asked my self and did't find anything about this topic, so I will try it in the best Laravel community all over the world - here at Laracasts.

Did you guys ever tried to alias Eloquent model columns? I'll give you an example: Let's say we have a table called "customers" and that table has three columns: "customer_name", "age_of_me", ''current_status".

I could create an Eloquent model that looks like this:

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model {

    protected $fillable = [];
    public $timestamps = false;

}

When I like to get all customers using return Customer::all(); I'll get a Collection containing all of those customers with the corresponding fields "customer_name", "age_of_me", ''current_status".

But now assume, that I don't like these field names and I can't rename them because other Enterprise solutions are using them. As a go(o)d developer I wanna have a clean and easy to understand codebase, so I like to get the field "name", "age", "status" in return.

Do you know any possibility to assume that, but without using the query builder and doing things like select customer_name as name ... from? A property like protected $aliases would be nice. What do ya think?

0 likes
16 replies
bart's avatar
Level 13

Nope, sorry but unfortunately this is not the solution. I know getters and setters or like Laravel calls them "acccessors" and "mutators". But I don't like to access a propery of the model. I like to get the properties by different keys than the column names, i.e. when getting a collection of obejects.

1 like
bart's avatar
Level 13

First of all thanks a lot @JarekTkaczyk But I'm using Lumen instead of Laravel 5 for a simple and fast API service. Does Lumen support additional Composer packages and will your package work with Lumen as well?

bart's avatar
Level 13

It seems that the $maps variable doesn't have any effect:

// Definition
protected $maps = [
    'number' => 'bad_number_column',
    'customer_number' => 'bad_customer_number_column',
    'customer_name' => 'bad_customer_name_column',
    'date' => 'bad_date_column',
];

// dd(Customer::all()->first()->toArray())
[
    'bad_number_column' => 123,
    'bad_customer_number_column' => 'XYZ12345',
    'bad_customer_name_column' => 'An amazing company name',
    'bad_date_column' => '2015-05-25 18:33:00',
]

Do you have an idea?

bart's avatar
Level 13

@JarekTkaczyk Short update: I did try it with Laravel 5 instead of Lumen, too. Same result. Maybe I'm doing something wrong?

JarekTkaczyk's avatar
Level 53

@bart If you want to simply rename the columns and use the aliases in toArray representation, use this mix:

$maps = ['bad_number_column' => 'number', 'bad_date_column' => 'date'];

$hidden = ['bad_number_column', 'bad_date_column'];

$appends = ['number', 'date'];

then you will get:

// dd(Customer::all()->first()->toArray())
[
    'number' => 123,
    'date' => '2015-05-25 18:33:00',
    ...
]

I assume this is what you wanted. Let me know otherwise.

3 likes
bart's avatar
Level 13

Yeah, that is what I want, thank you @JarekTkaczyk. I will try it out as soon as I'm back home today but I'm pretty sure it will work :)

bart's avatar
Level 13

Just for your information @JarekTkaczyk: I tried your solution and it works well. But one last question: How can I add an accessor to a mapped field? Here is an example:

protected $maps = [
    'number' => 'Number',
    'customer_number' => 'CustomerNumberOnBill',
    'customer_name' => 'The customer name',
    'date' => 'Date of invoice',
];
protected $visible = ['number', 'customer_number', 'customer_name', 'date'];
protected $appends = ['number', 'customer_number', 'customer_name', 'date'];

public function getDateAttribute() {
    return 'foo';
}

Customer::all()->first()->date returns the original date, but I'm expecting "foo" here.

Any idea? Thanks a lot!

1 like
JarekTkaczyk's avatar

@bart :)

It's impossible to achieve with generic mutators, because of the order of things that must happen there. Thing is, eloquent returns null (or in your case foo) and then it's mapped, so the value is overriden. Order cannot be changed.

But, what I can suggest is using Mutable:

// your model
use Eloquence, Mappable, Mutable;

// maps, hidden like before, additionally:
protected $getterMutators = [
    'date' => 'Your\Model@theMutator'
];

public function theMutator($date)
{
    return 'foo';
}

In place of theMutator you can use any of:

  • internal php function
  • user global function
  • public static method on any class
  • public instance method on any instantiable class
  • extension (macro) on the Sofa\Eloquence\Mutator

Plus you can use pipe separated multiple functions: strtolower | ucwords | theMutator.

To extend the mutator do something like:

// in a service provider:
$this->app['eloquence.mutator']->macro('ucname', function ($name) {
    return preg_replace_callback('/(?<=^|\-|\s)([a-z])/', function ($matches) {
        return strtoupper($matches[0]);
    }, strtolower($name));
});
// anywhere else:
app('eloquence.mutator')->...
bart's avatar
Level 13

Thank you so much @JarekTkaczyk :) As yesterday I'll try it out as soon as possible today. Thought it won't work due to the request chronology of Laravel. Found you Mutable extension on github but no documentation in the Wiki so I thought it hasn't been finished yet. Good to know, that this is not the case :)

JarekTkaczyk's avatar

@bart Everything that is pushed to the package repo is already 99-100% complete, but the docs are not :) I'm preparing a few short vids, since it is by far the easiest way to get a grasp of how things work.

1 like
bart's avatar
Level 13

Just for your information and I'm pretty sure you know it: IT WORKS!! Thanks again @JarekTkaczyk. A really useful package with interesting traits you built there. Amazing!

2 likes
carestad's avatar

I have been looking at this today, but I'm struggling to have this work in Laravel 5.5 without having to define the setters (setFooAttribute()) and getters (getFooAttribute()) . Is that even possible?

Please or to participate in this conversation.