44

I have an eloquent model named Eloquent:

Products::where("actice", "=", true)->get()->toArray();

Now I want to add join-statement to it, I have defined a scopeQuery with:

public function scopeJoinWithTags($query)
    {
        return $query->leftJoin("tags", "tags.id", "=", "products.tag_id");
    }

Then our main query changes to:

Products::where("actice", "=", true)->joinWithTags->get()->toArray();

What I get is OK, it is what I do expect, but I want to change the name property of tags table to tag_name, how should I do that? I mean, i say somewhere in my query to:

 tags.name AS tag_name

So that in the final result array I do :

$result[$i]['tag_name'];

While now I have to :

$result[$i]['name'];
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • possible duplicate [http://stackoverflow.com/questions/17174837/laravel-4-eloquent-column-alias](http://stackoverflow.com/questions/17174837/laravel-4-eloquent-column-alias) – itachi Nov 16 '14 at 15:08

4 Answers4

78

Simplest way to do this would be to add the fields you need to the get() method and alias the ones you want to rename there.

Products::where("actice", "=", true)
    ->joinWithTags
    ->get(['tags.name AS tag_name', 'products.*'])
    ->toArray();
Alexander Malakhov
  • 3,383
  • 2
  • 33
  • 58
turntwo
  • 2,452
  • 19
  • 28
  • Can you please tell me what is the second param? ''actice.*'` ? I have changed it to `products.*` and it works. – Mostafa Talebi Nov 17 '14 at 09:23
  • 2
    Oops sorry, yeah it should have been products.* It simply means to include all the fields of your product table. – turntwo Nov 17 '14 at 09:25
  • This is the method I used, but it should be noted that other frameworks handle this natively with associative arrays. It's a pity this is left out of Laravel. – dKen Feb 11 '16 at 08:19
  • This works well. But what if i joined a few tables and i want to get every table's column? do you put '*' in the 'products.*'? cuz i tried that and didnt work... – ichinisanshi May 20 '23 at 04:39
19

On Laravel 5.4 (I don't know if earlier version also apply) you can do that with the select method:

Products::where("actice", "=", true)
    ->joinWithTags
    ->select('tags.name AS tag_name', 'products.*')
    ->get();

At least for me this is cleaner.

Disturb
  • 558
  • 8
  • 14
18

There is also a cleaner way to achieve this

You can take advantage of laravel mutators

public function getTagNameAttribute()
{
    return $this->attributes['name'];
}

Hope this helps

FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66
0

This is my go-to. Good for backward compatibility with an old column name.

Add the following to your model

  1. protected $appends = ['old_column_name'];
  2. public function getOldColumnNameAttribute() {
      return $this->attributes['new_column_name'];
    }
    
    public function setOldColumnNameAttribute($value) {
      $this->attributes['new_column_name'] = $value;
    
      return $this;   
    }
    
tylersDisplayName
  • 1,603
  • 4
  • 24
  • 42