Selecting carefully with Laravel Joins
When joining two tables in Laravel, you have to be specific about what you're selecting.
Laravel joins
Let's say you have two tables, users
and posts
. Both have id
columns, and posts
has a user_id
that is a foreign key to the users
table.
To join the two tables together, you use the #join
method
$user = User
::join('posts', 'posts.user_id', '=', 'users.id')
->first();
But the results you get may be surprising
print $user->id; //prints the id of the posts table
The Problem
The query generated by a join clause uses a select all by default, so you can accidentally override columns
SELECT * FROM users INNER JOIN posts ON posts.user_id = users.id
It results in two id
columns returned, and internally the id
attribute is set to the second column (from the posts
table).
The solution
To work around this issue, make sure you are specific with your select clause
$user = User
::join('posts', 'posts.user_id', '=', 'users.id')
->select('users.*')
->first();
Now the results are what you would expect
print $user->id; //prints the id of the users table
Written by JP Camara
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#