Laravel Table Joining - php

I have three tables .
users
id name
roles
id name
role_user
id user_id role_id
I want to join them like
id name name(role) user_id role_id
My vision is to show the role name of the user.
I have tried this :
DB::table('users')
->join('role_user', 'users.id', '=', 'role_user.user_id')
->join('roles', 'role_user.role_id', '=', 'roles.id')
->select('users.id', 'role_user.role_id', 'roles.name')
->get();
Can you please tell me what I am doing wrong or if I am going thru a bad way.
Thanks in Advance

The name field is "double". This is not a problem for your database but it will not display well. Try the following code:
$result = DB::table('..etc.')->get(['users.*','role_user.role_id','roles.name as role_name']);
foreach($result as $row) {
print_r($row->toArray());
}
As you can see I've changed the get() call to make sure that the user's "name" field does not clash with the role's "name" field.

Related

Laravel Eloquent - query joint tables contains a same col name

I considered an issue when viewing data called from database using two related tables users and roles which have this structure:
the users.role column contains an integer referring to the role record of roles table
the roles.name column contains the role name i.e. (user / admin / editor)
in my controller I used the laravel docs to build my query as I need to show a table in blade that holds users.name , users.email and roles.name that related to user by the schema:
$users = DB::table('users')
->join('roles', 'users.role', '=', 'roles.id')
->select('users.name', 'users.email', 'roles.name')
->get();
actually it works and dumping data, but the issue is the confusion of the two name alike cols users.name and roles.name. it dumping only the roles.name value like this sample record:
{"name":"user","email":"user#asd.com"},{"name":"user","email":"new#asd.com"},{"name":"user","email":"jaeden93#example.org"}
even when I tried to select all cols of users table like below:
$users = DB::table('users')
->join('roles', 'users.role', '=', 'roles.id')
->select('users.*', 'roles.name')
->get();
the users.name col still not showing!
Is there a way to solve this issue without changing table's column titles ?
You can give an alias to the column, to avoid problems with the same name
Use this
$users = DB::table('users')
->join('roles', 'users.role', '=', 'roles.id')
->select('users.*', \DB::raw('roles.name as role_name'))
->get();
It's the same to do this in SQL
SELECT users.*, roles.name as role_name FROM ...

Laravel 5 - compare query from one table to another query

I have two tables: a relationship table and a users table.
Relationship table looks like: 'user_one_id', 'user_two_id', 'status', 'action_user_id'.
Users table looks like: 'id', 'username'.
I would like to query the relationship table first and return an array of all the rows where the 'status' column = 0.
Then I would like to query the users table and return an array of ids and usernames where 'user_one_id' matches 'id'.
My code so far:
public function viewRequests()
{
$currentUser = JWTAuth::parseToken()->authenticate();
$friendRequests = DB::table('relationships')
->where('user_two_id', '=', $currentUser->id)
->where('status', '=', '0')
->get();
$requestWithUsername = DB::table('users')
->where('id', '=', $friendRequests->user_one_id)
->get();
return $requestWithUsername;
}
It's not working and I'm not sure what method is easiest to reach my desired output. How can I change these queries?
EDIT:
After reviewing the response, this is the working code:
$friendRequests = DB::table('users')
->select('users.id','users.username')
->join('relationships', 'relationships.user_one_id','=','users.id')
->where('relationships.status','=',0)
->where('relationships.user_two_id', '=', $currentUser->id)
->get();
Your SQL seems to be this:
SELECT id, username
FROM users
JOIN relationships
ON relationships.user_one_id = id
WHERE relationships.status = 0
Then the Laravel way:
DB::table('users')
->select('id','username')
->join('relationships', 'relationships.user_one_id','=','id')
->where('relationships.status','=',0)
->get();

Laravel 5.1: handle joins with same column names

I'm trying to fetch following things from the database:
user name
user avatar_name
user avatar_filetype
complete conversation_messages
with the following query:
static public function getConversation($id)
{
$conversation = DB::table('conversation_messages')
->where('belongsTo', $id)
->join('users', 'conversation_messages.sender', '=', 'users.id')
->join('user_avatars', 'conversation_messages.sender', '=', 'user_avatars.id')
->select('users.name', 'conversation_messages.*', 'user_avatars.name', 'user_avatars.filetype')
->get();
return $conversation;
}
It works fine so far, but the avatar's column name is 'name' like the column name from the 'users' table.
So if I'm using this query the to get the output via $conversation->name, the avatar.name overwrites the users.name
Is there a way to rename the query output like the mysql "as" feature at laravel 5.1?
For example:
$conversation->avatarName
$conversation->userName
Meh okay.. i've found a simple solution here
->select('users.name as userName', 'conversation_messages.*', 'user_avatars.name as avatarName', 'user_avatars.filetype')
As you can mention I've added the requested "as-Feature" next to the table.columnName
Take a look at this example of trying to join three tables staffs, customers and bookings(pivot table).
$bookings = \DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')
->with('bookings', $bookings);
I had the following problem, simplified example:
$result = Donation::join('user', 'user.id', '=', 'donation.user_id')->where('user.email', 'hello#papabello.com')->first();
$result is a collection of Donation models. BUT CAREFUL:
both tables, have a 'created_at' column. Now which created_at is displayed when doing $result->created_at ? i don't know. It seems that eloquent is doing an implicit select * when doing a join, returning models Donation but with additional attributes. created_at seems random. So what I really wanted, is a return of all Donation models of the user with email hello#papabello.com
solution is this:
$result = Donation::select('donation.*')->join('user', 'user.id', '=', 'donation.user_id')->where('user.email', 'hello#papabello.com')->first();
Yeah, simply rename the column on either table and it should work.
Also what you can do is, rename the user.name column to anything, also rename sender column of conversation_messages to id and perform a natural join.

Join tables with equal columns

I have two tables users and cars, the cars has a column owner that is the id of an user and I need to join them. Both users and cars has a column called name and it's overriding. I want to keep both but with different names, like car_name and user_name on the return.
Here is how I'm doing:
$columns = [
DB::raw('cars.name as car_name')
];
$cars = Cars::join('users', 'users.id', '=', 'cars.owner')->get($columns);
It works if I try to print car_name but it still overriding the name and if I print name it still returns the name of the user instead of the car.
Is there a way for doing this right?
You can use the select() clause directly in your query.. no need to use DB::raw():
$cars = Cars::join('users', 'users.id', '=', 'cars.owner')
->select('*','cars.name as car_name')
->get();
The problem with this is that you will end up with a repeated column as you are asking the DB for everything, plus the field with a new name. That may not be a problem for you, but might be better to specify explicitly the fields you want to select:
$cars = Cars::join('users', 'users.id', '=', 'cars.owner')
->select('users.name as user_name','cars.name as car_name', 'whatever_else')
->get();
I don't think you can do what you want with gets - but you can with select. This should hopefully work:
$columns = [
'*',
'users.name as user_name',
'cars.name as car_name'
];
$cars = Cars::join('users', 'users.id', '=', 'cars.owner')->select($columns)->get();

Laravel get my friend list query

I am using laravel to create a friend list. Now I want to get all of my friends with the name and avatar and stuff from the USER table.
My schema:
`cid`, `uid_by`, `uid_to`, `status`
uid_by (the inviting user)
uid_to (to be friend user)
status (accepted or ignored)
I only have 1 row per friendship. meaning its a 2 way friendship.
I tried to make a query:
$contacts = DB::table('contacts')
->leftJoin('users', 'contacts.uid_by', '=', 'users.uid')
->where('uid_by', $me)
->orWhere('uid_to', $me)
->where('contacts.status', 'accepted')
->get();
But its giving me some wrong information. Like instead of giving me the friend user info. its giving me my own info. All I want is to get the friends info.
Should I redesign the db? or am I missing something?
update
technically the thing is if uid_by is my user_id then check the uid_to else if the uid_by is not my user_id then check uid_by then use those to get the data from the users table uid column.
So my uid could be on uid_by and my friend on uid_to or vice versa.
But I can't get the query right.
See Advanced wheres:
$contacts = DB::table('contacts')
->join('users', 'contacts.uid_by', '=', 'users.uid')
->where('users.uid','<>',$me)
->where('contacts.status', 'accepted')
->where(function($query)
{
$query-->where('uid_by', $me)
->orWhere('uid_to', $me)
})
->get();

Categories