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();
Related
I have two Models which are joined through three id's to one 1 id. The model AdviceProtocol had three ids defined: threshold_category1_id, threshold_category2_id and threshold_category3_id. These are all three linked to id from the model Categories. The threshold_cateogry_id's can be a number linked to the categories.id, or null.
In my query, I need to go through three of the threshold_catogory_ids and if they are NOT NULL, retrieve the categories.name. The query that I have now:
$advicePreparationsQuery = AdviceProtocol::select(['advice_protocols.name', 'advice_protocols.category', 'questions.name AS question_name', 'categories.name as Category_Name','categories.name as Category_Name2'])
->join('questions', 'advice_protocols.user_goal_id', '=', 'questions.id')
->join('categories', function ($join)
{
$join->on('advice_protocols.threshold_category1_id', '=', 'categories.id')->orOn('advice_protocols.threshold_category2_id', '=', 'categories.id')->orOn('advice_protocols.threshold_category3_id', '=', 'categories.id');
})
This query now returns the same category.name twice.
I have tried to look up how other people have solved this problem, but i couldn't find something accurate online.
I'd try three joins with three different alias and then grab the name for each category name of each different alias:
$advicePreparationsQuery = AdviceProtocol::select(['advice_protocols.name', 'advice_protocols.category', 'questions.name AS question_name', 'categories1.name as Category_Name','categories2.name as Category_Name2','categories3.name as Category_Name2'])
->join('questions', 'advice_protocols.user_goal_id', '=', 'questions.id')
->join('categories as categories1', 'advice_protocols.threshold_category1_id', '=', 'categories1.id')
->join('categories as categories2', 'advice_protocols.threshold_category2_id', '=', 'categories2.id')
->join('categories as categories3', 'advice_protocols.threshold_category3_id', '=', 'categories3.id');
Use group by name in query ie ->groupBy('category.name')
$advicePreparationsQuery = AdviceProtocol::select(['advice_protocols.name', 'advice_protocols.category', 'questions.name AS question_name', 'categories.name as Category_Name','categories.name as Category_Name2'])
->join('questions', 'advice_protocols.user_goal_id', '=', 'questions.id')
->join('categories', function ($join)
{
$join->on('advice_protocols.threshold_category1_id', '=', 'categories.id')->orOn('advice_protocols.threshold_category2_id', '=', 'categories.id')->orOn('advice_protocols.threshold_category3_id', '=', 'categories.id');
}
->groupBy('category.name'))
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 new to Laravel and I'm still working on my Project using this Framework. In my project, I use an SQL JOIN but I don't know how to retrieve/fetch data on the joined table.
Can anybody help me on this one?
I really appreciate your help. Thanks!
Here is a simple example!
$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();
$users = \DB::table('users')
->join('contacts', 'contacts.user_id', '=', 'users.id')
->join('orders', 'orders.user_id', '=', 'users.id')
->select('users.id as id', 'contacts.phone as phone', 'orders.price as price')
//Where condition here if exist
->get();
$users will now have the array of values.Loop it in an array if necessary.
Eg:
forearch($users as $user)
{ $user->id;
$user->phone;
}
Like that.
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()
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);