I am trying to generate a rank report in tabular form, I have three columns Rank, Distributor name, Total sales, the rank column is for obviously the rank in integer which the person ranks on the query but if two people have the same amount of sales they should have the same rank (int value), thirdly and most importantly sales column, what I am trying to achieve here is get the total sales for each of the distributors(names) .
Purchaser: Purchaser of the order. Distributor: The person who referred the Purchaser. Sales are the total amount of orders that have been purchased by the customers and distributors they have referred.
so for each order I have to check for the purchaser then get his/her referrer which is called the Distributor then get the distributors total sales which is the total in price x quantity of product purchased by all his referred purchasers(people referred by the distributors)
I have 6 tables in my database orders order_items products users user_category
below are pictures of the database structure
categories [1]: https://i.stack.imgur.com/SgzoU.png users [2]: https://i.stack.imgur.com/wkdN0.png orders [3]: https://i.stack.imgur.com/jlFRN.png products [4]: https://i.stack.imgur.com/Fvnds.png order_items [5]: https://i.stack.imgur.com/YWoGS.png user_category [6]: https://i.stack.imgur.com/9pk1m.png
so this is what i have tried on my blade file but doesn't return any results just times out
<table class="table table-striped custom-table" id="myTable">
<thead>
<tr>
<th scope="col">Rank</th>
<th scope="col">Distributor Name</th>
<th scope="col">Sales</th>
</tr>
</thead>
<tbody>
@foreach ($distributors as $dist)
@php
$user = \App\Models\User::where('id', $dist->user_id)->first();
foreach ($distributors as $dist) {
$user = \App\Models\User::where('id', $dist->user_id)->first();
$referrals = \App\Models\User::where('referred_by', $dist->user_id)->get();
foreach ($referrals as $ref) {
$quer = \App\Models\Order::where('purchaser_id', $ref->id);
}
$order_ids = $quer->get();
if (count($order_ids) > 0) {
foreach ($order_ids as $id) {
$items = \App\Models\OrderItem::where('order_id', $id->id);
}
$order_items = $items->get();
$quantities = $items->value('qantity');
foreach ($order_items as $orditem) {
$price = \App\Models\Product::where('id', $orditem->product_id)->value('price');
$total = $price * $orditem->qantity;
}
$sales += $total;
}
}
@endphp
<tr scope="row">
<td>
{{ $loop->iteration }}
</td>
<td>
{{ $dist->firstname }} {{ $dist->lastname }}
</td>
<td>
{{ number_format($sales) }}
</td>
</tr>
@endforeach
</tbody>
</table>
and in my controller returning the page
public function rank_report () {
$distributors = UserCategory::where('category_id', 2)->get();
$sales = 0;
return view('rank_report',compact('distributors', 'sales'));
}
also I would like to rank the result from highest to lowest first 100 of the results and display them on the table . Any other suggestion of a better way to do this will also be appreciated
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.