[英]Laravel query with joins, same name in multiple tables
I have a large query. 我有一个很大的查询。 For all intents and purposes, I'll recreate the issue using a smaller query as an example: 出于所有目的和目的,我将使用一个较小的查询作为示例来重新创建该问题:
DB::table('cases')
->join('contacts', 'cases.id', '=', 'contacts.id')
->select('cases.name', 'contacts.name')
->get();
And then in laravel's templating (Blade) when I do a foreach loop and print the results, it's printing the results from the cases table for {{ $case->name }}
然后在laravel的模板(刀片)中,当我执行一个foreach循环并打印结果时,它从case表中打印{{ $case->name }}
I understand the problem and why it is happening, but how can I make it work properly? 我了解问题及其原因,但是如何使它正常运行?
This is a PDO restriction and has nothing to do with laravel. 这是PDO的限制,与laravel无关。 If you need them both, you'll have to alias it in your query: 如果同时需要它们,则必须在查询中为其加上别名:
DB::table('cases')
->join('contacts', 'cases.id', '=', 'contacts.id')
->selectRaw('cases.name as cases_name, contacts.name as contacts_name')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.