relationship of 3 tables in laravel - php

Given that I have 3 tables: user, competences and competences_user, how can I get the list of skills of each user with query-builder?
Table structures:
user: id, nom....
competences: id, titre...
competences_user : id, competence_id, user_id...
Here is what I have tried so far:
select user.id, user.nom, competences.titre
FROM user
Inner Join competences_user
ON = competences_user.competence_id AND user.id = competence_user.user_id
Inner Join competences
ON competences.id = competence_user.competence_id

There is many ways to do it , the easy way without the query builder :
ie :
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
To answer your question :
$users = DB::table('users')
->join('competences_user', 'competences_user.user_id', '=', 'users.id')
->join('competences', 'competences_user.competence_id', '=', 'competences.id')
->select('users.*', 'competences.titre')
->get();
You can refer to the documentation for more details.

Related

Laravel 8 | How to join two foreign keys on the same table

Fields 'to_user_id' and 'from_user_id' are two id that are on the users table, since they have different values how to join my model 'Message' with the users table
$my_inbox=Message::select('messages.*', 'users.name toUser', 'users.name as fromUser')
->join('users', 'users.id', '=', 'to_user_id.user_id')
->join('users', 'users.id', '=', 'from_user_id.user_id')
->get();
Use différent aliases to join same table. You see that selecting the same field is conflicting with what you want.
$my_inbox=Message::select('messages.*', 'receiver.name toUser', 'sender.name as fromUser')
->join('users as receiver', 'receiver.id', '=', 'to_user_id.user_id')
->join('users as sender', 'sender.id', '=', 'from_user_id.user_id')
->get();
Or if you had set the correct relation in the Message class and the user class
$my_inbox = $loggedUser->receivedMessages()->with('sender')->get();

Laravel join query is showing wrong results

I have 4 tables clinics , locations , services & location_services in which relations are clinicid is present in locations and both serviceid and location id is present in location_services table.
My requirement is that i want to retrive all the cinics and their coreesponding locations and services .
But when i tried it is retrving the results of only one clinic id , i dont know how can i retrive complete list
following is my code
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid','locations.locationName as locname')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->toSql();
dd($locationservices);
die();
Please help me to solve this
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

Laravel 5 - DB join is returning just first table column set

I have some related MySQL tables, and I want to obtain the columns in all of them, but if I try with something like this code from the manual, I only get the columns from the first table:
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
That would be all columns from 'users' table, but I do not see contacts and orders columns.
try this
DB::table('users')->join('contacts',function($join){$join->on('users.id','=','contacts.user_id')})->join('orders',function($join){$join->on('users.id','=','orders.user_id')})->get()

DISTINCT method using Laravel 5 eloquent query builder

I'm struggling to get unique results when using the DISTINCT method in laravel. This is my query
//Get users that are part of this sector
$users = DB::table('users')->distinct()
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->get();
dd($users);
The result shows 3 users with the same ID, when I only want one of them in the results.
How should I change my query?
Try to use group by id
$users = DB::table('users')
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->groupBy('users.id')
->get();
dd($users);

laravel 4 - eloquent join

I try to show a list of users can add how friend with eloquent and Laravel 4.
I try to exclude the same user, and the currently friends
these are the tables/models
Users
id
first_name
last_name
genre
...
Friends
id
user_id
friend_id
status enum('status', array('PENDING', 'FRIENDS', 'DENIED'))
With the next code:
return DB::table('users')
->select('users.id',
'users.first_name',
'users.last_name',
'users.genre',
'friends.status')
->join('friends', function($join)
{
$join->on('users.id', '<>', 'friends.user_id')
->on('users.id', '<>', 'friends.friend_id');
})
->where('friends.status','<>', 'FRIENDS')
->get();
But not works, also i try this code
return DB::table('users')
->leftjoin('friends', 'users.id', '<>', 'friends.user_id')
->select('users.id',
'users.first_name',
'users.last_name',
'users.genre',
'friends.status')
->whereNotIn( 'users.id', array(Auth::user()->id) )
->get();
but neither works

Categories