如何在 Laravel Eloquent 查询(或使用查询生成器)中为表添加别名?
本文介绍了如何在 Laravel Eloquent 查询(或使用查询生成器)中为表添加别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们正在使用 Laravel 的查询构建器:
Lets say we are using Laravel's query builder:
I'm looking for an equivalent to this SQL:
This would be especially helpful when I have to type a lot of selects and wheres (or typically I include the alias in the column alias of the select as well, and it get's used in the result array). Without any table aliases there is a lot more typing for me and everything becomes a lot less readable. Can't find the answer in the laravel docs, any ideas?
解决方案
Laravel supports aliases on tables and columns with AS
. Try
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 Eloquent 查询(或使用查询生成器)中为表添加别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!