How to loop multiple nested loop in one table in laravel? - php

I have referral commission details and different levels of referral users I need to show that in the table with there are levels. Here I did coded but I'm not satisfied with the logic please let me know the logic is correct or wrong you see the below images for reference.
What i'm looking for
Here my table
What i did so far
Controller
public function inDirectCommission()
{
$user = User::where('id', 1)->first();
$user_id = $user->id;
$direct_commissions = Referral::where('parent_id', $user_id)->get();
foreach ($direct_commissions as $row) {
$second_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($second_level as $row) {
$third_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($third_level as $row) {
$forth_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($forth_level as $row) {
$fifth_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($fifth_level as $row) {
}
}
}
}
}
return view('site.user.dashboard.commission.in_direct_commission', compact('second_level', 'third_level', 'forth_level', 'fifth_level'));
}
Blade
<table id="example1"
class="table table-bordered table-striped dataTable dtr-inline direct-table"
role="grid" aria-describedby="example1_info">
<thead>
<tr>
<th>Date</th>
<th>Referal</th>
<th>Level</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach ($second_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>2<sup>nd</sup> Level</td>
<td>4000</td>
</tr>
#endforeach
#foreach ($third_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>3rd<sup>rd</sup> Level</td>
<td>3000</td>
</tr>
#endforeach
#foreach ($forth_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>4<sup>th</sup> Level</td>
<td>2000</td>
</tr>
#endforeach
#foreach ($fifth_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>5<sup>th</sup> Level</td>
<td>1000</td>
</tr>
#endforeach
</tbody>
</table>
dd of second level user

IMPORTANT: as per my comments under the question, this answer is to emphasize the idea only. Code samples and concepts might need to be adjusted to match your needs.
Assume there are users A,B,C,D,E and F and each has ids 1,2,3,4,5 and 6 respectively.
Also let's assume 'A' was a directly registered user. And:
A refers B to the website
B refers C to the website
C refers D to the website
D refers E to the website, and
E refers F to the website
With each user record, you can store their referral chain (ref_chain) in the database. At the time of user registration you can do this.
$a->ref_chain = null; // because he hasn't been referred by anyone
$b->ref_chain = '1'; // B (id = 2) is a direct referral of A (id = 1)
$c->ref_chain = '2-1'; // C (id = 3) is a direct referral of B (id = 2)
$d->ref_chain = '3-2-1'; // D (id = 4) is a direct referral of C (id = 3)
// similarly
$e->ref_chain = '4-3-2-1';
$f->ref_chain = '5-4-3-2-1';
Here, what you do is, at the time of registration you get the referring user's ref_chain and attach referring user's id to to the beginning of the string with a hyphen. For an example if we consider the time of C's registration:
$referringUser = $b;
$c->ref_chain = $b->id.'-'.$b->ref_chain;
Now consider the scenario where you want to get the referrals of C.
What you have to do is search in the ref_chain column in the database using the SQL like keyword;
$referralsOfC = User::where('ref_chain','like',$c->id.'-%')
->orWhere('ref_chain','like','%-'.$c->id.'-%')
->orWhere('ref_chain','like','%-'.$c->id)
->orWhere('ref_chain',"$c->id")
->get();
After that, you can loop through the collection and display the result as you please. Depending on the position of C's id in the ref_chain attribute you can recognize which level the referral is in.
This way you can drastically reduce the number of database queries and traversal.
Hope you can understand.

Related

Select values from a table where Ids are in json format in another table in laravel

I have a table named journal_details and it has a column named transaction_by_to. I am saving account_head_id values as json_encode format in transaction_by_to in table journal_details. Now I want to run a select query to get account head names from account_heads where transaction_by_to ids in JSON format will find the id from account_heads. In the controller, I tried something like bellow, but it's getting only one. But I want to show all from account_heads for all JSON formatted ids.
public function ledgerSubmit(Request $request)
{
$acId = $request->acount_head;
$startDate = Carbon::parse($request->start_date)->format('Y-m-d');
$endDate = Carbon::parse($request->end_date)->format('Y-m-d');
$reportType = $request->report_type;
$nameOfAccount = AccountHead::find($acId);
$ledgers = DB::table('journal_details')
->join('journals', 'journal_details.journal_id', '=', 'journals.id')
->join('account_heads', 'journal_details.account_head_id', '=', 'account_heads.id')
->where('journal_details.account_head_id', $acId)
->select('journals.number as journalNo', 'journals.journal_date as journalDate', 'journal_details.*', 'account_heads.name as nameOfAccount')
->get();
if (count($ledgers)) {
$transactionByToId = $ledgers[0]->transaction_by_to;
foreach (json_decode($transactionByToId) as $tId) {
$transactionByTo = AccountHead::where('id', $tId)->get();
}
}
return view('report.ledger.searched-result', compact('reportType', 'startDate', 'endDate', 'ledgers', 'transactionByTo', 'nameOfAccount'));
}
And in blade view-
#foreach($ledgers as $ledger)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $ledger->journalNo }}</td>
<td>{{ date('jS F, Y', strtotime($ledger->journalDate)) }}</td>
<td>{{ $ledger->nameOfAccount }}</td>
<td>
{{ $transactionByTo[0]->name }}
</td>
<td>
{{ number_format($ledger->debit, 2,".",",") }}
</td>
<td>
{{ number_format($ledger->credit, 2,".",",") }}
</td>
</tr>
#endforeach
["16","17","7","11"] is transaction_by_to column values in json format in journal_details and these ids are id for account_heads table.
There are several errors in your code, like in following code:
foreach (json_decode($transactionByToId) as $tId) {
$transactionByTo = AccountHead::where('id', $tId)->get();
}
it will always keep single value, the last value. So it will seem that there is only one record.
But why don't you do following to get all records at a time:
$accountHeadIds = json_decode($transactionByToId, true);
$accountHeads = AccountHead::whereIn('id', $accountHeadIds)->get();
Secondly, I don't see you are processing Account Head variable (as per my code example, $accountHeads) in the view file that is not part of $leders variable. If you want to have that under $ledgers variable then you should store under it.
Note: I haven't tested the code above, but the concept is similar.

Laravel Blade - orderBy

I have this foreach loop in my blade template.
#foreach ($employee->projects->groupBy('stage') as $stage => $project)
<tbody>
<td>{{ $stage }}</td>
<td class="text-center">{{ count($project) }}</td>
</tbody>
#endforeach
Where it finds all the projects that the employee is involved in and groups them by what stage they are in (Waiting, In Progress, Launched). And then it lists the stages and the count of how many are in each stage. This works perfectly but they are not in the right order. I would like them in Waiting, In Progress, Launched. Currently it's in Launched, Waiting, In Progress.
How can I order them in my custom order?
SQL should be
SELECT ... FROM projects GROUP BY stage
ORDER BY stage = 'Waiting' DESC, stage = 'In Progress', ...
probably you need use similar to this
->order_by(DB::expr("stage = 'Waiting' DESC"))->order_by(...)
Ok figured this out. Here's what I did...
$projects = $employee->projects()
->groupBy('stage')
->select('stage', DB::raw('count(*) as total'))
->orderByRaw('case
when stage = "Waiting" then 1
when stage = "In Progress" then 2
when stage = "Launched" then 3
end')
->get();
Then in my blade template, I did...
#foreach ($projects as $project)
<tbody>
<td>{{ $project->stage }}</td>
<td class="text-center">{{ $project->total }}</td>
</tbody>
#endforeach
Hope this helps someone else.

Enumerating elements of a table

I have a table and I want to enumerate each element.
Something like this
#foreach($elements as $element)
<tr>
<td>{{ (isset($a))?$a++:($a = 1) }}</td>
<td>...
</tr>
#endforeach
I expect the table to begin with 1 and then count on, however, the first two columns are always 1. I have already solved this problem by giving the template an $a = 0; on the controller but I want to know why my first solution doesn't work and if there is a workaround
Laravel Blade has a very handy variable for loops, called the $loop variable. This lets you gather information about the current element of the list, including the index and iteration count.
So you can do just this:
#foreach($elements as $element)
<tr>
<td>{{ $loop->iteration }}</td>
<td>...
</tr>
#endforeach

HREF sends parameters to controller and returns a View

So I am currently working on a project where I have a table that shows an overview of managers and the number of applications they are assigned to that currently have a certain status. What I am trying to do is when the user clicks on a number (which is the count) it returns a list view that queries from the database ONLY those applications that are assigned to that manager and that status. Otherwise, if the user goes to the application view, it should show ALL applications.
Here is the code of the View for my overview:
<table class="table tablesorter" id="tblAffs">
<thead>
<tr class="tblhead">
<th>AM</th>
<th>Qualified Prospect</th>
<th>Unqualified Prospect</th>
<th>Contacted</th>
<th>Converted To Account</th>
<th>Pending Integration</th>
<th>Revenue Generating</th>
</tr>
</thead>
<tbody>
#foreach ($totalCount as $id => $name) {{-- id is the admin id --}}
#foreach($name as $n => $status) {{-- $n is the name, $status is array of the counts --}}
<tr>
<td>
{{$n}}
<br>
Closed
</td>
<td>{{ isset($status[2]) ? $status[2] : 0 }}</td>
<td>{{ isset($status[1]) ? $status[1] : 0 }}</td>
<td>{{ isset($status[3]) ? $status[3] : 0 }}</td>
<td>{{ isset($status[4]) ? $status[4] : 0 }}</td>
<td>{{ isset($status[5]) ? $status[5] : 0 }}</td>
<td>{{ isset($status[6]) ? $status[6] : 0 }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
On my controller for this overview this is the code and how data structure created:
public function overview()
{
$query= DB::table('admins')
->join('advertiser_applications', 'admins.id', '=', 'advertiser_applications.assigned_to')
->selectRaw('advertiser_applications.status, admins.id, admins.first_name, COUNT(advertiser_applications.status) count')
->groupBy('admins.id', 'advertiser_applications.status');
$counts = $query->get();
$totalCount = [];
foreach($counts as $count){
$totalCount[$count->id][$count->first_name][$count->status] = $count->count;
}
return View::make('admin.crm.overview', ['totalCount' => $totalCount, 'admin_id' => AuthAdmin::admin(false), 'tpl_title' => 'CRM Advertiser Overview', 'new_lead' => 'advertisers']);
}
Here is the code from my Controller for my view that brings up the list. This is where I am confused as to how I will pass in that status and id if that href is clicked.
public function index($param)
{
$query = AdvertiserApplication::with('admin');
$status = Input::get('status');
if (!empty($status) || $status === '0')
$query->where('status', '=', $status);
$applications = $query->get();
return View::make('admin.advertisers.application_list', ['applications' => $applications, 'admin_id' => AuthAdmin::admin(false)]);
}
Now I am not sure if that is the proper way to link that I have written in my href. Should I do a URL link to route or something like that? I am not sure yet. Any tips would be much appreciated!
With this your URL:
{{ isset($status[2]) ? $status[2] : 0 }}
If you have
route::get('/ap1/{new_lead}', array('as' => 'page-name', 'uses' => 'yourController#functionName'));
in controller:
public function functionName($new_lead){
// use $new_lead here to get your data
}

How to: foreach without affecting every row?

Is there a way to do a foreach on for example your products and sort this by their category_id and show the category name above the products. (I'm using laravel 4 as framework)
Example what the output should look like:
Chairs
wooden chair number 1
pretty chair number 1
pretty chair number 2
Doors
tall door number 1
white door number 1
Key question: How can i make a foreach without having the categoryname above each row ?
Hopefully i made my question/problem clear and can someone help me out!
This is what i got so far:
Though, when i do my foreach in the blade i will get the error: " Undefined index "
The foreach i used in my controller:
foreach($results as $result) {
$category[$result->category_id][] = $result;
}
My foreach in the blade:
#foreach ($category as $key => $product)
<tr>
<td>{{ $product[$key]['created_at'] }}</td>
<td>{{ $product[$key]['order_id'] }}</td>
<td>{{ $product[$key]['name'] }}</td>
</tr>
#endforeach
you are attempting to use category_id to fetch $product from $products, which is array with 0-X indexes. for sake of simplicity i will ignore html formatting and syntax since i never used framework mentioned...
#foreach ($category as $key => $products)
echo 'Category #', $key
#foreach ($products as $product)
<tr>
<td>{{ $product['created_at'] }}</td>
<td>{{ $product['order_id'] }}</td>
<td>{{ $product['name'] }}</td>
</tr>
#endforeach
#endforeach
not completely sure i understand the question. but if im trying to organize an array.. i might use a conditional statement inside the foreach loop. I'm not sure your question is very clear.. see if this helps at all
foreach($results as $result) {
// just an example.. i narrow down what I push into my category array
if($result->type == 'whatever'){
$category[$result->category_id][] = $result;
}
}

Categories