I am trying to get "shifts" and get all records I have,
But I join it with orders and run where on orders by delivery_date here "the shift" which has no orders erased
now I need to make the where only on orders and not affect on shifts
Shift::leftJoin('mt_plans', 'mt_plans.shift_id', '=', 'shifts.id')
->leftJoin('subscriptions', 'subscriptions.plan_id', '=', 'mt_plans.plan_id')
->join('orders', 'orders.subscription_id', '=', 'subscriptions.id')
->join('mt_order_details', 'mt_order_details.order_id', '=', 'orders.id')
->join('mt_item', 'mt_item.item_id', '=', 'mt_order_details.item_id')
->where('orders.delivery_date', $request->date)
->whereIn('orders.status', [OrderStatus::ACCEPTED, OrderStatus::ASSIGNED, OrderStatus::FAILED, OrderStatus::SUCCESSFUL])
->select([
'mt_item.item_id as meal_id',
'mt_item.en_name',
'mt_item.color',
'mt_item.label_number',
'shifts.id as shift_id',
'shifts.title',
'shifts.start_time',
'shifts.end_time',
'shifts.pickup_time'
])
->get();
You need a left join where your condition applies:
Shift::leftJoin('mt_plans', 'mt_plans.shift_id', '=', 'shifts.id')
->leftJoin('subscriptions', 'subscriptions.plan_id', '=', 'mt_plans.plan_id')
->leftJoin('orders', function($q) use($request) {
$q->on('orders.subscription_id', '=', 'subscriptions.id');
$q->where('orders.delivery_date', '=', $request->date);
})
->leftJoin('mt_order_details', 'mt_order_details.order_id', '=', 'orders.id')
->leftJoin('mt_item', 'mt_item.item_id', '=', 'mt_order_details.item_id')
->whereIn('orders.status', [OrderStatus::ACCEPTED, OrderStatus::ASSIGNED, OrderStatus::FAILED, OrderStatus::SUCCESSFUL])
->select([
'mt_item.item_id as meal_id',
'mt_item.en_name',
'mt_item.color',
'mt_item.label_number',
'shifts.id as shift_id',
'shifts.title',
'shifts.start_time',
'shifts.end_time',
'shifts.pickup_time'
])
->get();
Then you keep all "shifts" but join only order with delivery_date condition.
Keep in mind that it will retrieve all "shifts" where where condition applies. The orders will be null if doesn't match the id and delivery_date
Related
I have this query:
$counts = DB::table('projects')
->join('prject_links', 'prject_links.project_id', '=', 'projects.id')
->join('links', 'links.id', '=', 'prject_links.link_id')
->join('segments', 'segments.id', '=', 'links.segment_id')
->join('hthree_regions', 'hthree_regions.id', '=', 'segments.hthree_id')
->join('areas', 'areas.id', '=', 'hthree_regions.area_id')
->join('zone_regions', 'zone_regions.id', '=', 'areas.zone_id')
->select(
'zone_regions.id as regions',
'areas.id as provinces',
'hthree_regions.id as cities',
'segments.id as segments'
)
->get();
what I'm looking to have as result is something like:
counts => [
regions => 20,
provinces => 10,
cities => 7,
segments => 5
];
What do i get currently is confusing result like:
any idea about how to fix my query?
Use groupBy and count(distinct field) to get the field's count in each project:
->selectRaw(
"CONCAT('project ', projects.id) as project,
COUNT(DISTINCT zone_regions.id) as regions,
COUNT(DISTINCT areas.id) as provinces,
COUNT(DISTINCT hthree_regions.id) as cities,
COUNT(DISTINCT segments.id) as segments"
)
->groupBy('projects.id')
->get();
This might be your solution. Run it and let me know if that helps
$counts = DB::table('projects')
->join('prject_links', 'prject_links.project_id', '=', 'projects.id')
->join('links', 'links.id', '=', 'prject_links.link_id')
->join('segments', 'segments.id', '=', 'links.segment_id')
->join('hthree_regions', 'hthree_regions.id', '=', 'segments.hthree_id')
->join('areas', 'areas.id', '=', 'hthree_regions.area_id')
->join('zone_regions', 'zone_regions.id', '=', 'areas.zone_id')
->groupBy('projects.id')
->selectRaw(
'count(zone_regions.id) as regions',
'count(areas.id) as provinces',
'count(hthree_regions.id) as cities',
'count(segments.id) as segments'
)
->get();
I have 4 tables:
notices (id, title, desc, is_published)
notices_to_districts (id, notice_id, district_id)
notices_to_employees (id, notice_id, user_id)
notices_to_establishments (id, notice_id, establishment_id)
I want to get all unique notices if,
notices_to_districts has a specific district_id, or
notices_to_employees has a specific user_id, or
notices_to_establishments has a specific establishment_id
I am trying -
$notices = Notice::join('notices_to_districts', 'notices_to_districts.notice_id', '=', 'notices.id')
->join('notices_to_establishments', 'notices_to_establishments.notice_id', '=', 'notices.id')
->join('notices_to_employees', 'notices_to_employees.notice_id', '=', 'notices.id')
->where('district_id', '=', $user_district_id)
->orWhere('establishment_id', '=', $user_establishment_id)
->orWhere('user_id', '=', $user_id)
->where('is_published', '=', 1)->get();
You should use Closure Parameter Grouping or else, your "orWhere" will not work as you wish.
$notices = Notice::join('notices_to_districts', 'notices_to_districts.notice_id', '=', 'notices.id')
->join('notices_to_establishments', 'notices_to_establishments.notice_id', '=', 'notices.id')
->join('notices_to_employees', 'notices_to_employees.notice_id', '=', 'notices.id')
->where('district_id', '=', $user_district_id)
->where('is_published', '=', 1)
->where(
function ($query) use ($user_establishment_id, $user_id) {
$query->orWhere('establishment_id', '=', $user_establishment_id)
->orWhere('user_id', '=', $user_id);
}
)
->get();
Like this example, the SQL will have the correct parentesis around your clause.
I solved it -
$notice_ids_of_district = Notices_to_district::select('notice_id')->where('district_id', '=', $user_district_id)->get();
$notice_ids_of_establishment = Notices_to_establishment::select('notice_id')->where('establishment_id', '=', $user_establishment_id)->get();
$notice_ids_of_employee = Notices_to_employee::select('notice_id')->where('user_id', '=', $user_id)->get();
$notices = Notice::whereIn('id', $notice_ids_of_district)
->orWhereIn('id', $notice_ids_of_establishment)
->orWhereIn('id', $notice_ids_of_employee)
->get();
I want to get data from table role_users , column role_id . For the moment, I have this in my controller :
$data['contact_users'] = DB::table('contacts')
->join('users' , 'users.id', '=', 'contacts.contact_id')
->join('industries' , 'industries.id', '=', 'users.industry_id')
->join('countries' , 'countries.id', '=', 'users.country_id')
->join('organization_types' , 'organization_types.id', '=', 'users.organization_type_id')
->select('users.*','industries.industry','countries.country','organization_types.organization_type')
->where('contacts.contact_id','!=',$id)
->where('users.deleted_at','=',NULL)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I'm using it in view with this code :
{{$contact->industry_id}} or {{$contact->country_id}}
I need to use something like this.
{{$contact->role_id}}
which is working for every user.But I need to get data from role_users,column role_id.I don't know how to use ->join() and I need it so much.Thank you.
I added one extra join and one select field at the end,
$data['contact_users'] = DB::table('contacts')
->join('users', 'users.id', '=', 'contacts.contact_id')
->join('industries', 'industries.id', '=', 'users.industry_id')
->join('countries', 'countries.id', '=', 'users.country_id')
->join('organization_types', 'organization_types.id', '=', 'users.organization_type_id')
->join("role_users", "role_users.user_id","=","users.id")
->select('users.*', 'industries.industry', 'countries.country', 'organization_types.organization_type', "role_users.role_id")
->where('contacts.contact_id', '!=', $id)
->where('users.deleted_at', '=', null)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I use to PHP and Laravel Framework on my project.
Above code works perfectly
$users = User::select([
'users.id',
'users.name',
'users.company',
'users.country',
'users.city',
'users.email',
'users.created_at',
\DB::raw('SUM(reservations.dolar) as dolar'),
\DB::raw('count(reservations.confirmation) as confirmation'),
])->join('reservations','reservations.user_id','=','users.id')
->groupBy('reservations.user_id');
Now Counting all reservation.confirmation column but I want to count only reservation.confirmation column values 1
How I can edit
\DB::raw('count(reservations.confirmation) as confirmation'),
this code
Have you tried see in https://laravel.com/docs/5.6/queries#joins in the section "Advanced Join Clauses", you can add condition in Join function
$users = User::select([
'users.id',
'users.name',
'users.company',
'users.country',
'users.city',
'users.email',
'users.created_at',
\DB::raw('SUM(reservations.dolar) as dolar'),
\DB::raw('count(reservations.confirmation) as confirmation'),
])->join('reservations', function ($join) {
$join->on('reservations.user_id', '=', 'users.id')
->where('reservations.confirmation', '=', 1);
})->groupBy('reservations.user_id');
If you want have all sum dolar not depends with confirmation,
you can use 'case' statement:
\DB::raw('sum(case when reservations.confirmation=1 then 1 else 0 end) as confirmation')
or subquery:
\DB::raw('(Select count(*) FROM reservations WHERE user_id = users.id AND confirmation=1) as confirmation')
I have the following raw SQL that I'm trying to convert into Eloquent form:
SELECT oe.eventID, oe.eventName, oet.etName, date_format(oe.eventStartDate, '%c/%d/%Y') as eventStartDate,
date_format(oe.eventEndDate, '%c/%d/%Y') AS eventEndDate
FROM `org-event` oe
JOIN `org-event_types` oet on oe.eventTypeID=oet.etID and oet.orgID=?
JOIN `event-registration` er on er.eventID = oe.eventID
WHERE (er.regStatus='Active' or er.regStatus='In Progress') AND personID=? AND oe.deleted_at is NULL
ORDER BY oe.eventStartDate DESC
I've crafted the following and I'm running into an error saying that '10' isn't a column. 10 is the value of $this->currentPerson->defaultOrgID
DB::table('org-event')
->join('org-event_types', function($join) {
$join->on('org-event_types.etID', '=', 'org-event.eventTypeID');
$join->on('org-event_types.orgID', '=', $this->currentPerson->defaultOrgID);
})->join('event-registration', 'event-registration.eventID', '=', 'org-event.eventID')
->where('event-registration.personID', '=', auth()->user()->id)
->where(function($w) {
$w->where('event-registration.regStatus', '=', 'Active')
->orWhere('event-registration.regStatus', '=', 'In Progress');
})
->select('org-event.eventID', 'eventName', 'etName', 'eventStartDate', 'eventEndDate')
->orderBy('org-event.eventStartDate')->get();
I dropped the deleted_at where clause because that's automatic using eloquent.
You have to join on the corresponding foreign key: oe.eventTypeID=oet.etID and then supply a where explaining what you want to filter your results by, i.e. ->where('orgID','=',$this->currentPerson->defaultOrgID).
DB::table('org-event')
->join('org-event_types', function($join) {
$join->on('org-event_types.etID', '=', 'org-event.eventTypeID');
$join->on('org-event_types.orgID', '=', 'org-event.eventTypeID')->where('orgID','=',$this->currentPerson->defaultOrgID);
})->join('event-registration', 'event-registration.eventID', '=', 'org-event.eventID')
->where('event-registration.personID', '=', auth()->user()->id)
->where(function($w) {
$w->where('event-registration.regStatus', '=', 'Active')
->orWhere('event-registration.regStatus', '=', 'In Progress');
})
->select('org-event.eventID', 'eventName', 'etName', 'eventStartDate', 'eventEndDate')
->orderBy('org-event.eventStartDate')->get();