Home > Backend Development > PHP Tutorial > php laravel DB join field name modification

php laravel DB join field name modification

WBOY
Release: 2016-09-15 11:30:59
Original
1836 people have browsed it

laravel

<code>$houses = DB::table('houses')->join('status', 'houses.status_id', '=', 'status.id')->get()</code>
Copy after login
Copy after login
There is a

status_id field in the houses table, and the corresponding name field in the status table needs to be queried;

And what we get in $houses is the name field in the status table. How to change the name to the status_name name?

Supplement

This is currently the case: houses There is no name field in the table, so there is nothing wrong when checking the status name name,

You can directly take out the name field from the status table, but what to do if the houses table also has a name field.

Because the title field is built in the houses table. If you add a name field to the houses table and check it in the join status table, an error will occur!

Reply content:

laravel

<code>$houses = DB::table('houses')->join('status', 'houses.status_id', '=', 'status.id')->get()</code>
Copy after login
Copy after login
There is a

status_id field in the houses table, and the corresponding name field in the status table needs to be queried;

And what we get in $houses is the name field in the status table. How to change the name to the status_name name?

Supplement

This is currently the case: houses There is no name field in the table, so there is nothing wrong when checking the status name name,

You can directly take out the name field from the status table, but what should be done if the houses table also has a name field.

Because the title field is built in the houses table. If you add a name field to the houses table and check it in the join status table, an error will occur!

<code>DB::table('houses')->select('*','name as status_name')->join('status', 'houses.status_id', '=', 'status.id')->get();</code>
Copy after login

<code class="sql">DB::table('houses')->select('name as status_name')->join('status', 'houses.status_id', '=', 'status.id')->get()</code>
Copy after login

As keyword, you can give it an alias

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template