Jak alias tabeli w Laravel Eloquent queries (lub za pomocą Query Builder)?
Powiedzmy, że używamy konstruktora zapytań Laravela:
$users = DB::table('really_long_table_name')
->select('really_long_table_name.id')
->get();
Szukam odpowiednika do tego SQL:
really_long_table_name AS short_name
Byłoby to szczególnie pomocne, gdy muszę wpisać wiele Select i wheres (lub zazwyczaj dołączam alias do kolumny alias select, a get jest używany w tablicy wyników). Bez aliasów tabel jest o wiele więcej pisania dla mnie i wszystko staje się o wiele mniej czytelne. Nie możesz znaleźć odpowiedzi w Laravel docs, jakieś pomysły?
5 answers
Laravel obsługuje aliasy tabel i kolumn za pomocą AS
. Try
$users = DB::table('really_long_table_name AS t')
->select('t.id AS uid')
->get();
Zobaczmy go w akcji za pomocą niesamowitego narzędzia tinker
$ 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' // ) // )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-09 04:52:49
Aby używać aliasów na wymownych modelach zmodyfikuj swój kod w następujący sposób:
Item
::from( 'items as items_alias' )
->join( 'attachments as att', DB::raw( 'att.item_id' ), '=', DB::raw( 'items_alias.id' ) )
->select( DB::raw( 'items_alias.*' ) )
->get();
Spowoduje automatyczne dodanie prefiksu tabeli do nazw tabel i zwróci instancję modelu Items
. Nie wynik zapytania.
Dodanie DB::raw
zapobiega dodawaniu przedrostków tabeli do aliasów.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-06 05:29:33
Oto jak można to zrobić. Podam przykład z dołączeniem, aby stało się to dla kogoś bardzo jasne.
$products = DB::table('products AS pr')
->leftJoin('product_families AS pf', 'pf.id', '=', 'pr.product_family_id')
->select('pr.id as id', 'pf.name as family_name', 'pf.id as family')
->orderBy('pr.id', 'desc')
->get();
Mam nadzieję, że to pomoże.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-11 15:09:48
Do użycia w wymowie. Dodaj do swojego modelu
protected $table = 'table_name as alias'
/ / table_name powinna być dokładna jak w Twojej bazie danych
..następnie użyj w zapytaniu jak
ModelName::query()->select(alias.id, alias.name)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-20 12:12:36
To samo co AMIB answer, dla błędu delete soft "Unknown column' table_alias.deleted_at"",
po prostu dodaj ->withTrashed()
a następnie zrób to sam jak ->whereRaw('items_alias.deleted_at IS NULL')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-01 16:41:44