I intend to pull all the rows that have same order ID from an orderDetail table using the get() method and loop through to add all the values of the tax column to a $total_ variable. Then I planned to add the $total_ to the seller balance.
I want to get the sum of the tax values in column that get pulled out with the same order_id. I have tried to use the sum() but there seem to still be error. The only thing that work is when I used first() which only get the first row where my condition is true. but that way, I am only able to use one tax value. There are instances where I have two items with the same order_id (when we have different products in a cart). They come into the database with same order_id. So, I am now looking for a way to pull all rows with same order_id and then get the sum of the tax column.
if ($request->status == 'cancelled' && $order->payment_status == 'paid') {
$commissionHistoriesnow = \App\Models\CommissionHistory::where('order_id', $request->order_id)->get();
$total__ = 0;
foreach ($commissionHistoriesnow as $key => $commissionHistorynow) {
if($commissionHistorynow->admin_commission != null) {
$total__ += $commissionHistorynow->admin_commission;
}
}
$seller = Seller::where('user_id', $commissionHistoriesnow->seller_id)->first();
$seller->admin_to_pay += $total__;
$seller->save();
}
You can use the sum method of the Builder to get a sum of that column for you without having to retrieve all the records and iterate them:
$total = CommissionHistory::where('order_id', $request->order_id)
->sum('admin_commission');
Additional:
Also, you could use the increment method on the Model to increment the 'admin_to_pay' field and update it in the database (which would fire Model events):
$seller->increment('admin_to_pay', $total);
If you are not worried about events and there is only the one record by this condition you could call increment on the builder itself to update the record you are querying for:
Seller::where('user_id', $commissionHistoriesnow->seller_id)->increment('admin_to_pay', $total);
Laravel 8.x Docs - Queries - Running Database Queries - Aggregates sum
Laravel 8.x Docs - Eloquent - Retrieving Single Models / Aggregates - Retrieving Aggregates sum
Laravel 8.x Docs - Queries - Update Statements - Increment & Decrement increment
Related
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
In my project I'm using Laravel 5.5 with Eloquent and Scout drivers to build a sort of search engine API endpoint.
In my scenario I have a SQL table items that has a price_factor property.
The table is also stored inside an Elasticsearch index.
With this value and with the number of the user related with that item, I can calculate the right price of the object.
An easy example is the item with id: 1 has price_factor: 2 and it is related to 5 users.
The right price of the item is 2 * 5 = 10, of course.
Now, I have to query all results and use where conditions, sorting them by that calcolated property, and return paginated results.
For example: get all items with price between 5 and 10, sort them by price and paginate by 10 elements per page.
In Eloquent I will write:
// Set filters to be applied
$filters = [
['price', '>', 5],
['price', '<', 10],
];
// Sort by "update_at", or "price"
$sort = "price";
// Order by "ASC" mode
$order = "ASC";
// Number of rows per page
$rows = 10;
// Get items
$result = Item::orderBy(
$sort,
$order
// Get related user with item record, where has...
)->with(['users'])->whereHas(
// Take users related models
'users',
// Take the model related ("user") and use filters
function($relation_model) use ($filters) {
// Apply where condition with filters
$relation_model->where($filters);
}
// Paginate for the numbers of row requested
)->paginate($rows);
How to do that if price is not a property of table items?
Should I store price inside the table and update it on every new user relation added? (or every removed relation too).
Is this the correct approach?
I was thinking about website like eBay or other real-time auction that have a similar situation of mine: how do you think they have solved?
Thanks in advance.
Assuming that you have a user_items table that keeps track of the items owned by user, I think something like this might work:
$result = Item::selectRaw('items.*, price_factor * (SELECT COUNT(id) FROM user_items WHERE user_items.item_id = items.id) AS price')
->with(['users'])
->havingRaw('price > ? AND price < ?', [5, 10])
->orderBy('price', 'asc')
->paginate($rows);
You can calculate the price on the fly and alias it. Finally you can apply a havingRaw clause to it that will check if the price is between the range it needs to be. Here's a working example of this in action.
There might be better ways to do this. I am also curious to know :)
I have related items in my database. I selected all of items from database by related id:
$next_stock = $this->model->get()->where('part_id', $in_data['part_id'])->all();
and I collection of rows grouped by one specific id, like on the picture. All of them selected by "part_id":
Selection Of Items
Grouped By Same Id
Also with this line of code i can select one of the items from this collection:
$next_stock = $this->model->get()->where('id', $old_stock['id'])->where('part_id', $in_data['part_id'])->first();
But how can I select the following items after this one?
Or, how can I select second or third item from this collect?
I cannot just increase id number by one from first, because sometimes this item ids not following each other.
Having a collection, you can take a specific element in the position with a combination of take() and last().
$collection = $this->model->get()->where('part_id', $in_data['part_id'])->all();
$second = $collection->take(2)->last(); //if this doesnt work, do it in 2 steps
$third = $collection->take(3)->last(); //if this doesnt work, do it in 2 steps
If you don't have a collection, take directly from database like this
$second = $this->model
->where('part_id', $in_data['part_id'])
->skip(1)
->first();
If it doesn't work with first()
$collect = $this->model
->where('part_id', $in_data['part_id'])
->skip(1)
->take(1)
->get();
$second = $collect->first();
Edit
skip() and take() are actually part of the query builder, not eloquent model. So it won't work with Eloquent in Laravel 5.4
Try with
$collect = $this->model
->where('part_id', $in_data['part_id'])
->get(1); //For the second record, 0 being the first
If you aren't doing it yet, you should set your model's relationships.
E.g. If you use "one-to-many", Eloquent will automatically determine the proper foreign key column on the model for you.
$parts = App\Stock::find(1)->partId;
foreach ($parts as $part) {
//
}
I'm new to Laravel and here's my issue.
I have a table currentpercentage.
This is the structure of the table currentpercentage
currentpercentage(**id**, name, total_cap, current_usage, created_at, updated_at)
I'm trying to calculate percentage of current usage; based on total_cap and current usage.
total_cap = 1000,
current_usage = 237,
name = User Name
In my controller i've setup a query to get the value of total_cap and the value of current_usage then calculate that the percentage would be.
When i call my query, it returns an array with the column name (total_cap) and value (1000). Same as when i query for current_usage.
{
"currentpercentage": [
{
"total_cap": 1000
}
]
}
I just want the query to return just the number (1000) without the array.
This is my query
$totalcap = CurrentPercentageModel::select('total_cap')->where('name', '=', 'User Name')->get();
How do I just get the value. Or is there an easier way to calculate the percentage from one query?
CurrentPercentageModel //What I use to connect to the database. DB Model
The problem is that you are using the get method which returns a collection even when you only have one row.
$totalcap = CurrentPercentageModel::select('total_cap')->where('name', '=', 'User Name')->get();
If you just want one record and one column value, then use the value method to get just the value of the column, more info here (you might have to scroll a little bit)
$totalcap = CurrentPercentageModel::select('total_cap')->where('name', '=', 'User Name')->value('total_cap');
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";