sum incorrect after using join in Eloquent Laravel - php

I had to join two tables together and after some trial and error I was able to get it, but while trying to sum the joined field to later sort by that quantity I realized the numbers were much higher than what they are supposed to be. I tried to follow this question's solution but it didn't work for me: The sum amount from join tables is incorrect . Here is my query down below:
$query = Item::has('backorders')
->join('SOP10200', 'IV00102.ITEMNMBR','=','SOP10200.ITEMNMBR')
->select('IV00102.ITEMNMBR',
//These sums are wrong when using join
Item::raw("SUM(IV00102.QTYONHND) as qty"),
Item::raw("SUM(IV00102.QTYONORD) as ordered"),
Item::raw("SUM( ( CASE WHEN IV00102.LOCNCODE LIKE 'IT-%' THEN IV00102.QTYONHND END ) ) as transit"),
Item::raw("SUM(SOP10200.QUANTITY) as backorder"),
)
->where('IV00102.PRIMVNDR', Auth::user()->vendor_id)
->groupBy('IV00102.ITEMNMBR')
->orderBy($group['field'], $group['sort'])
->limit(2147483647);
Here are my relationships:
public function item(){
return $this->belongsTo(Item::class, 'ITEMNMBR', 'ITEMNMBR');
}
public function backorders(){
return $this->hasMany(Backorder::class, 'ITEMNMBR', 'ITEMNMBR')->where('SOPTYPE', 5);
}
I'd like to note that I could not use the relational identifiers in the join and had to resort to referencing the tables directly, but the relationships work otherwise, and have been tested without the join. In other words the whole reason I am joining is so that I can sort by the backorders(SOP10200). if there is a more elegant solution to this I am all for it. To avoid repeated offered solutions I'd like to also attach my previous question related to and solving the join issue: Is there a way to select fields from an eager loaded table in Laravel?

I solved this by implementing a subquery, doing sum on the subquery and then finally joining it to the main query:
$subQuery = Backorder::select('ITEMNMBR', Backorder::raw('sum(QUANTITY) as backorder'), 'SOPTYPE')
->groupBy('ITEMNMBR', 'SOPTYPE');
$subQuerySql = $subQuery->toSql();
$query = Item::has('backorders')
->where('IV00102.PRIMVNDR', Auth::user()->vendor_id)
->leftjoin(Item::raw('(' . $subQuerySql . ') as bbo'),function($join) use ($subQuery) {
$join->on('IV00102.ITEMNMBR', '=', 'bbo.ITEMNMBR');
})
->where('bbo.SOPTYPE', 5)
->select('IV00102.ITEMNMBR',
'bbo.backorder',
Item::raw("SUM(IV00102.QTYONHND) as qty"),
Item::raw("SUM(IV00102.QTYONORD) as ordered"),
Item::raw("SUM( ( CASE WHEN IV00102.LOCNCODE LIKE 'IT-%' THEN IV00102.QTYONHND END ) ) as transit"),
)
->mergeBindings($subQuery->getQuery())
->groupBy('IV00102.ITEMNMBR', 'bbo.backorder')
->orderBy($group['field'], $group['sort'])

Related

Laravel, search exact same substring in eloquent query

I have a table like this
Teacher Table
What I am trying to do is to get the row which contains the subjects 1(or any other number like 7,8 etc.)
This is what I have tried in my controller.
public function allTeachers($sub_id) //receiving $sub_id(to be searched)
{
$teachers_all=Teacher::where('subjects','like','%'.','.$sub_id.'%')->latest()->paginate(50);
dd($teachers_all);
}
The problem here is that, I am getting all the rows which contains subjects as '1',e.g. if it is '3,11,22' or '41,5' it gets selected.
But what I am trying to achieve is it should only return where subjects string contains '1' followed by any other number after ',' or '1,2,44,31,23' etc.
I am using laravel, I hope I made the question clear.
The solution to your question would be either to use find_in_set or concat to fill some missing commas and then search for the value:
Teacher::whereRaw('find_in_set("' . $sub_id . '", subjects) <> 0')
->latest()
->paginate(50);
or
Teacher::whereRaw('concat(",", colors, ",") like "%,' . $sub_id . ',%"')
->latest()
->paginate(50);
That being said, #bromeer's comments hold true in any case. MySQL isn't around comma-separated values in fields. Both examples shown above aren't an ideal solution. You should look into relationships a bit more.
I suggest using a many-to-many relationship in your case. For that, create a pivot table called teacher_subject and add the relation to your Teacher model:
public function subjects()
{
return $this->belongsToMany(Subject::class);
}
To find any teachers teaching a specific subject, use whereHas like this:
Teacher::whereHas('subjects', function (Builder $query) use ($sub_id) {
$query->where('id', $sub_id);
})->latest()->paginate(50);

Laravel pagination with specific foreign key

I need to make pagination in Laravel, and as I read laravel documentation here is a several way to do it. but in my cases it is a little bit complexity. So let say I've two table in my DB, table1 and table2(many to many relationship), I want to paginate table1 with specific ids. So I mean if table1 contains ids 1,2,3,4,5 ... n, I want to paginate only rows which id is 2 and 3
I have tried :
private function check_wear($type){
$products_id = array(); //here is ids which $type is set as 'ON'
$wears = DB::select("SELECT prod_id FROM wears WHERE ".$type." = 'on' "); //Select specific products ids. (in our case it is **table1**)
foreach ($wears as $wr){
array_push($products_id,$wr->prod_id);
}
return $products_id; //return array with ids which should be paginate
}
public function hat($locale){
$hat_prod_ids = $this->check_wear('coat');
$p=12;
$products = DB::table('products')->paginate($p); // 12 per page
dd($products);
my code only paginate one table with all data, is there any built-in function to somehow write a logic condition? for pagination
If you need to filter your pagination with elements which you've in $hat_prod_ids, you're able to use WhereIn() method, that one checks if your elements (in our case ids) exist in your $hat_prod_ids and returns true or false
$products = DB::table('products')
->whereIn('id',$hat_prod_ids)
->paginate($p);
add that code and now you will be able to paginate only that ids which is matched in your array

How to use not equal query in Yii2

I have two tables ad_pool and advertisment. ad_pool has some data while advertisment is empty. I am using this code to select values from the first table by not equal query like this andWhere(['<>','fisttablekey','second_tbl_key']). This is the complete code i am using to retrieve the data and i also uploaded the image.
$pool1 = (new Query())>select('p.id,p.cleaner_user_id,p.ad_place_id')
->from('ad_pool p')
->innerJoin('advertisment a' , 'p.id = a.pool_id')
->where(['=','ad_place_id',1])
->andWhere(['<>','p.id','a.pool_id'])
->orderBy(new Expression('rand()'))
// ->limit(1)
->all();
var_dump($pool1);
exit();
this return me Empty array. need your kind help. Thanks in advance.
The INNER JOIN keyword selects records that have matching values in both tables.
Since your advertisement is empty, it won't return any data. You can use LEFT JOIN instead.
$pool1 = (new Query())>select('p.id,p.cleaner_user_id,p.ad_place_id')
->from('ad_pool p')
->leftJoin('advertisment a' , 'p.id = a.pool_id')
->where(['=','ad_place_id',1])
->andWhere(['<>','p.id','a.pool_id'])
->orderBy(new Expression('rand()'))
->all();
W3Schools Reference
This way I solved this problem after 3 to 4 hours hardwork. Get the required values from the second table if there is any and then convert it into single array and used it in the NOT IN condition. (we must have passion of helping others and we should help each other).

Counting total distant relationships in Laravel (eager loading?)

I'm having issues getting a proper count total with my Laravel model.
Model Structure
User
Item
ItemLike
A user can have multiple Items, and each of these Items can have multiple ItemLikes (when a user 'likes' the item).
I can easily get the individual ItemLike counts when using an Item model:
return $this->itemLikes()->count();
But I can't figure out how to get the total # of ItemLike's a User has across all the Item's he owns.
EXAMPLE
User A has 3 Items. Each Item has 5 ItemLike's, for a grand total of 15.
I tried using eager loading on the User model like this:
return $this->items()->with('itemlikes')->get()->count();
But that returns 3 (the # of Items)
These are the queries it ran, which appears like the second query is the one I want, yet every way I try it I still get 3 instead of 15
select * from `items` where `items`.`user_id` = '1000'
select * from `item_likes` where `item_likes`.`item_id` in ('1000', '1001', '1002')
After suggestions from others I found 2 solutions to get the result.
Using whereIn:
$itemViewCount = ItemView::
whereIn('item_views.item_id', $this->items()->lists('id'))
->count();
return $itemViewCount;
2 queries for a total of 410μs
Using join:
$itemViewCount = $this->items()
->join('item_views', 'item_views.item_id', '=', 'items.id')
->count();
return $itemViewCount;
2 queries for a total of 600μs
Isn't it just a case of creating a method that would return the number of items for the model. e.g.:
#UserModel
public function nbLikes()
{
$nbLikes = 0;
foreach($this->items() as $item) {
$nbLikes += $item->itemLikes()->count();
}
return $nbLikes;
}
And then User::nbLikes() should return the piece of data you are looking for?
try this:
$query="select count(il.id) from item_likes il,item itm where il.item_id=itm.id and tm.user_id=1000";

Summing over multiple fields in Laravel

I'm wondering if it is possible to take the sum of multiple fields in one query using the fluent query builder.
I currently have two tables: events and attendees. Attendees belong to events and have two fields: total_raised and total_hours. What I want to do is select all events and the total amount raised/total number of hours spent on that event. Now, if I were just using SQL I would do something to the effect of:
SELECT Event.id, sum(Attendees.total_raised), sum(Attendees.total_hours)
FROM Events JOIN Attendees ON Events.id = Attendees.event_id
GROUP BY Event.id
However, I can't seem to find a way to take multiple sums at once using the fluent query builder. Is there any way to do what I'm trying to do using fluent, or should I just make it a raw SQL query?
You can use sum() i.e.:
$q = DB::table('events')
->join('attendees', 'events.id', '=', 'attendees.event_id')
->sum('total_raised')
->sum('total_hours');
If that doesn't work you can try:
...
->get(
array(
'events.id',
DB::raw('SUM(attendees.total_raised)'),
DB::raw('SUM(attendees.total_hours)')
)
);
Building on simones answer. You could do this by essentially running two queries.
$query = DB::table('events')->join('attendees', 'events.id', '=', 'attendees.event_id');
$raised = $query->sum( 'total_raised' );
$hours = $query->sum( 'total_hours' );
It depends on the situation. If it were on the admin/CMS side of things I'd be lean towards this solution. If it is on the front end it should be done in a single query which will be faster. Depending on the content it may or may not be a significant difference.
$result = DB::table('events')->join('attendees', 'events.id', '=', 'attendees.event_id')
->get( array(
DB::raw( 'SUM(attendees.total_raised) AS raised' ),
DB::raw( 'SUM(attendees.total_hours) AS hours' ),
));
I am writing this answer to help those who are in search to sum multiple fields in a single table.
If you want to sum multiple fields inside a single table so there would be no need to "join" you can simply do it likewise, assuming the table like this.
In your controller do this:
$billInfo= Bills::where('reports_id',2)->get( array(
DB::raw('SUM(Price) as total_price'),
DB::raw('SUM(balance) as total_balance'),
DB::raw('SUM(paid) as total_paid'),
));
this will result the below data:
[{"total_price":17500,"total_balance":17500,"total_paid":null}]
I am doing the same thing in my project, Here is the solution which I found. I am using Laravel 5.2 Eloquent here is the Eloquent statement.
This statement which I use in my project, Please made change according to your need.
$result = self::select("*", DB::raw('SUM(auction_amount) as total_auction_amount') , DB::raw('SUM(commission_amount) as total_commission_amount'),
DB::raw('SUM(deposit_amount) as total_deposit_amount'))
->groupBy('cp_user_id')
->get()
->toArray();
Same way you can use for your query like
$result = self::select("*", DB::raw('SUM(auction_amount) as total_auction_amount') , DB::raw('SUM(Attendees.total_raised) as total_raised'),
DB::raw('SUM(Attendees.total_hours) as total_hours'))
->with('Attendees')
->groupBy('id')
->get()
->toArray();

Categories