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();
Related
I'm not that good with Laravel Eloquent, so be cool pls.
I have 1 product table and 4 "target" tables (sales, novelties, liquidations and exposure).
I'm trying to get all products that have at least one of the four "targets" with this query:
return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
->where('noveltys.novelty_end_at', '!=', null)
->orwhere('sales.sale_end_at', '!=', null)
->orWhere('liquidations.liquidation_end_at', '!=', null)
->orWhere('exposures.exposure_end_at', '!=', null)
->get();
Problem is, it only return products that have the first "where" clause true. (In other word, currently, this query only return produtcs with a not null novelty_end_at, it doesn't return products with a sale_end_at and others).
How can I achieve that it also return products with a not null sale_end_at, exposure_end_at and a liquidation_end_at ?
Thank you!
seems a syntax mistake
->orwhere('sales.sale_end_at', '!=', null)
should be
->orWhere('sales.sale_end_at', '!=', null)
also you can try using orWhereNotNull
like
return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
->whereNotNull('noveltys.novelty_end_at')
->orWhereNotNull('sales.sale_end_at')
->orWhereNotNull('liquidations.liquidation_end_at')
->orWhereNotNull('exposures.exposure_end_at')
->get();
I found the solution, it's almost the same thing, execpt that I'm using orWhereNotNull and that I'm using DB::table at the start instead of Product::leftjoin directly.
return DB::table('products')
->leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->whereNotNull('sales.sale_end_at')
->orwhereNotNull('noveltys.novelty_end_at')
->orwhereNotNull('liquidations.liquidation_end_at')
->orwhereNotNull('exposures.exposure_end_at')
->get();
Thank you!
Perhaps you could use ->orWhereHas() method. To use this function, you must define the relationship in your Sales and the four target Model classes. Documentation: https://laravel.com/docs/9.x/eloquent-relationships
Then in your query,
return Product::whereHas('noveltys', function(Builder $query) {
// your where clause
})
->orWhereHas('sales', function(Builder $query) {
// your where clause
})
->orWhereHas('liquidations', function(Builder $query) {
// your where clause
})
->orWhereHas('exposures', function(Builder $query) {
// your where clause
})
->get();
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
What I am trying to achieve is to allow teachers to import a student into different classes.
Note: A student can be multiple classes.
The problem is that when I show the list of students in a select dropdown it should return all students except for students that are not in this class (the class being the page that I am on, app.com/classes/5 for example).
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'asc')
->get();
This works and shows all students that are not in this specific class BUT if a student that's in this class and another class their name appears in the list and as duplicate names.
What can I do?
When MySQL's only_full_group_by mode is turned on, it means that strict ANSI SQL rules will apply when using GROUP BY
You should try to select fields from schema on which you can apply group by instead of select *.
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->select('users.id', 'other fields you used')
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
Not IN is also useful in your case
User::select('fields you used')
->role('student')
->whereNotIn('id', DB::table('group_user')->where('group_id', $id)->pluck('user_id')) // $id = 5
->orderBy('name', 'ASC')
->get();
Modify your query to use distinct() like so;
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->distinct()
->get();
You could also groupBy('users.id')
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->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 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();