Reputation: 6383
Is it possible to have multiple Aliases from a single table column. If I do the following in MySQL I get the result I want:
SELECT name, name AS label, name AS value FROM `tags` WHERE 1
But, if I try to do:
$query->select('name, name AS label, name AS value');
..it tells me SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name,' in 'field list' (SQL: select
name,as
ASfrom
tagswhere
tags.
deleted_atis null group by
nameorder by
nameasc)
If I try to do
$query->select('name AS name, name AS label, name AS value');
..it doesn't throw an error, but instead only gives me one alias (name). How can I achieve this using Eloquent ORM? Thanks
Upvotes: 0
Views: 1823
Reputation: 3608
When you try select laravel do this:
public function select($columns = array('*'))
{
$this->columns = is_array($columns) ? $columns : func_get_args();
return $this;
}
And when you pass the string, laravel thinks you passed few args, so you can do:
$query->select(['name AS name', 'name AS label', 'name AS value']);
Or
$query->selectRaw('name AS name, name AS label, name AS value'):
Or
$query->select(DB::raw('name AS name, name AS label, name AS value'));
Or like said @Jarek Tkaczyk
$query->select('name as name', 'name as label', 'name as value');
Upvotes: 2
Reputation: 500
Try this:
$query->select(DB::raw('name AS name, name AS label, name AS value'));
Upvotes: 0