Laravel Blade - orderBy - php

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.

Related

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

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.

Format number to K/M/B within blade template

I am using laravel 5.7.19.
Within my TableController.php controller I query data from my db and hand it over to the view:
public function index()
{
$c = DB::table('tick_data')
->select('*')
->join('basis', 'basis.Id', '=', 'tick_data.b_id')
->whereRaw('tick_data.id IN( SELECT MAX(tick_data.id) FROM tick_data GROUP BY tick_data.exchange_timestamp)')
->get();
return view('datatable')->with('coins', $c);
}
Within my table.blade.php file I am putting the data out:
#foreach ($coins as $key=>$c)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $c->pair }}</td>
<td>{{ number_format($c->last_price, 8) }}</td>
<td>{{ number_format($c->price_change_percentage, 8) }}</td>
<td>{{ number_format($c->price_change, 8) }}</td>
<td>{{ number_format($c->high_price, 8) }}</td>
<td>{{ number_format($c->low_price, 8) }}</td>
<td>{{ $c->base_volume }}</td>
<td>{{ $c->name }}</td>
</tr>
#endforeach
As you can see I am mainly using the number_format() function for formatting my values.
However, base_volume comes in the form of 467703.0000000000 or 10831.13202978000 and I would like to change it to the shortform with K,M,B.
Is there any function in blade that can do this? What is a good way to preformat the numbers?
Appreciate your replies!
I don't know any blade functions that can do this. However, you can use the PHP Humanizer package for this. For example, from their documentation:
use Coduo\PHPHumanizer\NumberHumanizer;
NumberHumanizer::metricSuffix(-1); // "-1"
NumberHumanizer::metricSuffix(0); // "0"
NumberHumanizer::metricSuffix(1); // "1"
NumberHumanizer::metricSuffix(101); // "101"
NumberHumanizer::metricSuffix(1000); // "1k"
NumberHumanizer::metricSuffix(1240); // "1.2k"
NumberHumanizer::metricSuffix(1240000); // "1.24M"
NumberHumanizer::metricSuffix(3500000); // "3.5M"
The good thing about the package is, it also supports multiple locales.
Edit: There are also other alternative solutions as discussed here. This could be a better option for you if you just need to shorten numbers.

Laravel Eloquent splitting query into array

Bit of a bad title, not quite sure how to research/describe what I'm trying to do.
I have a table called specifications with the fields
id, device_id, name, detail
Two rows for two different devices would for example be
1, 1, weight, 300kg
2, 2, weight, 250kg
Now the way I need to display it is in a single table compare both devices, essentially like this:
#foreach($specifications as $specification)
<tr>
<td>{{ $specification->name }}</td>
<td>{{ $specification->detailOne }}</td>
<td>{{ $specification->detailTwo }}</td>
</tr>
#endforeach
I'm having issues getting the right query or split the query in order to be able to go through the array like above. Anyone mind putting me on the right mindset? Do I query both specs and somehow resort it into an array I can use as above, or is there something else I should be looking at?
This should work for you
#foreach($specifications->groupBy('name') as $name => $specificationGrouped)
<tr>
<td>{{ $name }}</td>
#foreach($specificationGrouped as $specification)
<td>{{ $specification['detail'] }} ({{ $specification['device_id'] }})</td>
#endforeach
</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