Laravel Join query is not working for 4 tables - php

I have 4 tables
Table Name : Clinics
Fields: clinicID , clinicName
Table Name : locations
Fileds: locationID, clinicID,locationname
Table Name : Services
Fields: ServiceId , ServiceName
Table Name: LocationServices
Fields: locationServiceID, locationID , ServiceId
My requiremnt is that when i pass clinicID, i need to retrive Corresponding clinics service name, there may be more than one.
But when i tried join query is not working
Following is my code in controller
public function showClinic($id)
{
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationsservices=\App\Clinic::with('locations');
var_dump($locationsservices);
die();
return view('clinic.show')->with(['locations' => $locations ,'clinic'=>$clinic]);
}

You can get this details by using relationships.
In the Clinic model,add
public function locations()
{
return $this->belongsTo('App\Models\Locations','clinicID','clinicID');
}
In the Locations model,add,
public function location_services()
{
return $this->hasOne('App\Models\LocationServices','locationID','locationID');
}
On the LocationServices model,
public function services()
{
return $this->hasOne('App\Models\Services','ServiceId','ServiceId');
}
You can get the result by,
$clinic_info = Clinic::find($id);
if(isset($clinic_info->locations))
{
if(isset($clinic_info->locations->location_services))
{
if(isset($clinic_info->locations->location_services->services))
{
echo $clinic_info->locations->location_services->services->ServiceName;
}
}
}

Reference Maybe HelpFull for You
Multi Joins
$data = DB::table('city')
->join('state', 'state.state_id', '=', 'city.state_id')
->join('country', 'country.country_id', '=', 'state.country_id')
->select('country.country_name', 'state.state_name', 'city.city_name')
->get();
return view('join_table', compact('data'));

Related

how to join 4 tables in php laravel?

I Want to extract data from 4 tables .
like sname,cgpa,Group_Cgpa,Room_No and Hostel Name.
Relation of table is as follows.
AllotmentApps has : id,sname,cgpa and grpID (from Group id Table) columns.
Groups has : id,Group_CGPA
Room has : id, HostelID (From Hostel) and ResidentID (From Group id).
Hostels has: id, Name, Total_Rooms.
I'm facing problem in joining the tables. Join with AllotmentApps and Groups works fine, but problem occurs when I joined the Room and Hostel Table.
Join of AllotmentApps and Groups
public function FinalList()
{
$allot_apps = AllotmentApps::join('Groups','Groups.id','AllotmentApps.grpID')->orderBy('Groups.Group_CGPA', 'Desc')->get()->groupBy(
function($item) {
return $item->grpID;
}
) ->take(3);
Join of 4 Tables
public function FinalList()
{
$allot_apps = AllotmentApps::select('sname','cgpa','Group_CGPA','Hostel_Name')->join('Groups','Groups.id','AllotmentApps.grpID')->join('Hostels','Hostels.id','Hostels.HostelID')->join('Rooms','Rooms.id','Hostel.HostelID')->orderBy('Groups.id', 'Asc')->get()->groupBy(
function($item) {
return $item->grpID;
}
) ->take(3);
Try This
$allot_apps=AllotmentApps::select('AllotmentApps .sname','AllotmentApps .cgpa','Groups.Group_CGPA','tablename.Hostel_Name')
->join('Groups','Groups.id','=','AllotmentApps.grpID')
->join('Rooms','Rooms.ResidentID','=','Groups.id')
->join('Hostels','Hostels.id','=','Rooms.HostelID')
->orderBy('Groups.id', 'Asc')
->groupBy()
->get();
public function FinalList()
{ $capasity = Rooms::selectRaw('count(id) as c')->count();
$allot_apps = Rooms::select('Hostel_Name','Rooms.id','Groups.Group_CGPA','AllotmentApps.sname','AllotmentApps.cgpa')->join('Hostels','Hostels.id','Rooms.HostelID')->join('Groups','Groups.id','Rooms.ResidentID')->join('AllotmentApps','Groups.id','AllotmentApps.grpID')->orderBy('Groups.Group_CGPA', 'Desc')->get()->groupBy(
function($item) {
return $item->grpID;
}
) ->take($capasity);
// return $allot_apps;
return view('x', compact('allot_apps'));
}

laravel querying many to many

I have 2 models, Service and Category. They are related with a many-to-many relationship like so:
Service.php
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
Category.php
public function services()
{
return $this->belongsToMany('App\Service')->withTimestamps();
}
And of course they're joined by a pivot table:
category_service
- category_id
- service_id
- created_at
- updated_at
I'd like to use local scope to filter service result based on IDs of categories. I've done the following:
Service.php
public function scopeFilter($query, $category_ids)
{
$services = Service::whereHas('categories', function (Builder $query) use ($category_ids) {
$query->whereIn('category_id', $category_ids)->get();
});
return $services;
}
But I'm getting a Column not found error, specifically:
Column not found: 1054 Unknown column 'services.id' in 'where clause' (SQL: select * from `categories` inner join `category_service` on `categories`.`id` = `category_service`.`category_id` where `services`.`id` = `category_service`.`service_id` and `category_id` in (1, 2))
1 and 2 are the category IDs I pass.
I wrote the function based on the answer I found here and here.
Any pointers?
Your error message show that your query is begin with categories and without join services.
So put the ->get() outside the closure.
public function scopeFilter($query, $category_ids)
{
$services = Service::whereHas('categories', function (Builder $query) use ($category_ids) {
$query->whereIn('category_id', $category_ids);
})->get();
return $services;
}

Laravel get joined data according to condition for joined table column

Hi guys i am using laravel5.5
I have two tables Users and Services
Users Table
id
name
email
password
address
city
country
zipcode
Service table
id
User_id
name
description
price
In User Model
public function services()
{
return $this->hasMany('App\Service');
}
In Service Model
public function user()
{
return $this->belongsTo('App\User');
}
Now in controller I need all services where user->zipcode = 20006
How can i get it
I have tried this below code
$services = Service::with('user')->where('user->zipcode', '20006')->get();
But it did not work.
thanks in advance.
Warm Regards:
Abdullah Shahid.
$services = Service::with(['user'])->whereHas('user', function($q) {
$q->where('zipcode', '20006');
})->get();
Finally got it
$services = Service::whereHas('user', function($q)use($zipcode) {
$q->where('zipcode' , $zipcode);
})->get();

Laravel query builder join where

I've got this query:
$query = QueryBuilder::for(Advertisement::class)
->with('locations');
The locations method on Advertisement looks like this:
public function locations()
{
return $this->belongsToMany(Location::class, 'advertisement_locations', 'advertisement_id', 'location_id');
}
So a advertisements belongsToMany locations in between is a pivot table called advertisement_locations.
Now I would only get the advertisements between a given long and latitude that's on the locations table.
How could I do this?
Use this:
$query = Advertisement::whereHas('locations', function($query) {
$query->whereBetween('long', [$min, $max])
->whereBetween('latitude', [$min, $max]);
});

Laravel Lucid whereHas by latest column in pivot table

I have 3 tables : Orders ( id, name, surname, created_at and updated_at ), OrdersStatuses (order_id, order_status_name_id, created_at) and OrderStautsNames ( id and name ):
I have orders model which has method like this:
protected $appends = ['actual_status'];
public function orderProducts()
{
return $this->hasMany(OrderProduct::class);
}
public function statuses()
{
return $this->belongsToMany(OrderStatusName::class, 'order_statuses')
->withPivot('created_at');
}
public function getActualStatusAttribute()
{
return $this->statuses()->latest('set_at')->first();
}
I have problem with seraching all orders, where highest status id ( this info is in pivot table OrderStatuses, and I need name of this status which is in OrderStatusNames ) is like $statuses (this is an array with statuses ). I started do like this:
$orders = Order::query();
$orders->whereHas('statuses', function($query) use ($statuses) {
$query->whereIn('order_status_name_id', $statuses);
});
But it return me orders with not latest status, but this query search me throught all statuses... . Can i do this by latest status? I tryed to use current_status but I don't know how to get there.
Add this relation to your Order Model
public function lastStatus()
{
return $this->hasOne(OrderStatusName::class)->select('OrderStautsNames.*', 'order_statuses.order_id', DB::raw('MAX(order_statuses.set_at) as lastOrder'))->join('order_statuses', 'order_statuses.order_status_name_id', '=', 'OrderStautsNames.id')
}
Then eager load it
$orders = Order::whereHas('lastStatus', function ($query) use ($statuses) {
$query->whereIn('OrderStautsNames.id', $statuses);
})->get();

Categories