How to alias the name of a column in Eloquent
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();
Laravel: How to aliases a column name
You can call $someModel->toArray()
(or ->getAttributes()
, which will hide sensitive data such as passwords) to get the underlying data array. That way, you can print the column names and their values:
<ul>
@foreach($user->toArray() as $key => $attribute)
<li>{{ $key }}: {{ $attribute }}</li>
@endforeach
</ul>
But your keys will say e.g. "id", "name", etc., and not a longer word. I would suggest putting that in a translation file. Just as an example:
<ul>
@foreach($user->toArray() as $key => $attribute)
<li>{{ __('user-model-keys.'.$key) }}: {{ $attribute }}</li>
@endforeach
</ul>
resources/lang
contains a folder for every language you want your application in. Every file in there contains your translations as a returned array. So if you want to translate "id" as "Identification", and your language is English, then given the above code you create a file resources/lang/en/user-model-keys.php
and put this inside:
<?php
return [
'id' => 'Identification'
];
Now, __('user-model-keys.id')
will translate "id" to "Identification".
You can read more about localisation here: https://laravel.com/docs/5.6/localization
How to alias a table in Laravel Eloquent queries (or using Query Builder)?
Laravel supports aliases on tables and columns with AS
. Try
$users = DB::table('really_long_table_name AS t')
->select('t.id AS uid')
->get();
Let's see it in action with an awesome tinker
tool
$ php artisan tinker
[1] > Schema::create('really_long_table_name', function($table) {$table->increments('id');});
// NULL
[2] > DB::table('really_long_table_name')->insert(['id' => null]);
// true
[3] > DB::table('really_long_table_name AS t')->select('t.id AS uid')->get();
// array(
// 0 => object(stdClass)(
// 'uid' => '1'
// )
// )
Laravel column alias
You can use select.
<?php
$query = \DB::table('enquiries')
->join('user_masters', 'enquiries.user_master_id', '=', 'user_masters.id')
->join('items', 'enquiries.items_id', '=', 'items.id')
->select('enquiries.id as enquiries_id'); // and so on.
$enquiries = $query->get();
...
Eloquent column alias
The solution was to setup mutators in the model. even though the column name was in snake case I still had to use camel case for the function name. so SALES_ORDER_NUMBER became SalesOrderNumber. here is an example:
protected function getSalesOrderNumberAttribute($value) { return $this->attributes['SALES_ORDER_NUMBER']; }
protected function setSalesOrderNumberAttribute($value) { $this->attributes['SALES_ORDER_NUMBER'] = (is_null($value) ? '' : $value); }
Change column name in Laravel equivalent
You can write it like this one
Term::whereDate('begin_date', '>=', $start)
->whereDate('end_date', '<=', $end)
->get(['id','name','begin_date AS start_date', 'end_date As finish_date']);
Or by editing it in select
method
Term::whereDate('begin_date', '>=', $start)
->whereDate('end_date', '<=', $end)
->select('id','name','begin_date AS start_date', 'end_date As finish_date')
->get();
Related Topics
Preg_Match Equivalent in Swift
Resetting MySQL Root Password with Xampp on Localhost
Doctrine - How to Print Out the Real SQL, Not Just the Prepared Statement
How to Implode Array with Key and Value Without Foreach in PHP
Is "Set Character Set Utf8" Necessary
How to Include External CSS and Js File in Laravel 5
Proper Repository Pattern Design in PHP
Laravel Checking If a Record Exists
How to Check If a Directory Exists? "Is_Dir", "File_Exists" or Both
Check If Url Has Certain String with PHP
How to Get the Request Parameters in Symfony 2
Composer Require Local Package