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
Related
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();
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();
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.
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);
I have two tables, one with users, one with the name of their document. The first table consists of two columns: id and username. The second one consists of three columns: id, userid and document_name.
Now, I'm trying to create a query in the controller. What should happen, ideally, is that if someone visits website.com/{documentname}, it displays the username of the owner. Also, this should only happen if the current logged in user is the owner of the document. However, this is proving more difficult than I imagined. As in, I can't see what I'm doing wrong.
Here's the query:
$user = DB::table('documents')
->join('users', function($join)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
**Try this query :**
$user = DB::table('documents')
->leftJoin('users', 'users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
->get();
$document_name isn't in scope for the join function: you need to pass it through to the closure via use
$user = DB::table('documents')
->join('users', function($join) use ($document_name)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
EDIT
Because the WHERE conditions apply to the base table, and not to the JOIN:
$user = DB::table('documents')
->join('users', function($join) {
$join->on('users.id', '=', 'documents.userid')
})
->where('userid', '=', Auth::id())
->where('document_name', '=', $document_name);
->get();