php - How to alias a table in Laravel Eloquent queries (or using Query Builder)? -
lets using laravel's query builder:
$users = db::table('really_long_table_name') ->select('really_long_table_name.id') ->get();
i'm looking equivalent sql:
really_long_table_name short_name
this helpful when have type lot of selects , wheres (or typically include alias in column alias of select well, , get's used in result array). without table aliases there lot more typing me , becomes lot less readable. can't find answer in laravel docs, ideas?
laravel supports aliases on tables , columns as
. try
$users = db::table('really_long_table_name t') ->select('t.id uid') ->get();
let's see in action 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 t')->select('t.id uid')->get(); // array( // 0 => object(stdclass)( // 'uid' => '1' // ) // )
Comments
Post a Comment