Query leaderboard with ties - php

Controller:
$users = DB::table('users')
->join('uploads', 'users.id', '=', 'uploads.user_id')
->select(DB::raw('users.*, COUNT(*) AS total_uploads'))
->orderby('total_uploads', 'desc')
->groupby('users.id')
->take(5)
->get();
View:
<?php $i = 1 ?>
#foreach($users as $user)
<tr>
<td>{{ $i }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->total_uploads }}</td>
</tr>
<?php $i++ ?>
#endforeach
Expected output:
1 John 99
2 Mary 78
3 Jill 57
3 Bill 57
5 Hans 56
How can I modify this to account for ties?

Try this:
<?php $i = 0;
$last_count = null;
?>
#foreach($users as $user)
<?php if($user->total_uploads != $last_count) $i++; ?>
<tr>
<td>{{ $i }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->total_uploads }}</td>
</tr>
<?php $last_count = $user->total_uploads; ?>
#endforeach
The counter will only increment if the current user's total_uploads is different from the last user's total_uploads.

Related

How to i get gaji from table gaji when i choose grade

How to i get gaji from table gaji when i choose grade
if I choose grade A, the salary will be 1000000 automatically
Pegawai.blade.php
#php
$A = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
$B = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
$C = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
$D = DB::table('karyawan')->join('gaji','gaji.id', '=', 'karyawan.gaji_id')->->get('gaji.gaji');
#endphp
<tbody>
#foreach($ar_karyawan as $peg)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $peg->nip }}</td>
<td>{{ $peg->nama }}</td>
<td>{{ $peg->gender }}</td>
<td>{{ $peg->tgl_lahir }}</td>
<td>{{ $peg->tgl_masuk }}</td>
<td>{{ $peg->grade }}</td>
</tr>
#endforeach
</tbody>
</table>
PegawaiController.php
public function index()
{
$ar_karyawan = DB::table('karyawan')
->join('gaji', 'gaji.id', '=', 'karyawan.gaji_id')
->select('karyawan.*', 'gaji.gaji AS gaji')
->get();
return view('pegawai.index', compact('ar_karyawan'));
}
table pegawai
https://i.imgur.com/Iy6ev8l.png
table gaji
https://i.imgur.com/MTxrJ0g.png
You wrote the code the wrong way. You wrote duplicate "->->"
Change the following code
from
$A = DB::table('employee')->join('salary','salary.id', '=', 'employee.salary_id')->->get('salary.salary');
to
$A = DB::table('employee')->join('salary','salary.id', '=', 'employee.salary_id')->get('salary.salary');

Displaying text according to the ID of category LARAVEL

#foreach($products as $product)
<tr class="product">
<td>{{ $product->product_name }}</td>
<td>${{ $product->price }}</td>
<td>{{ $product->category }}</td>
<td>{{ $product->product_description }}</td>
<td>Delete</button></td>
#endforeach
Hi, how do program this code such that if the catagory_id = 1 "Sushi" is displayed and if catagory_id = 2 "Drinks" is displayed.
As of now this is the display:
enter image description here
Database
You should write like this
In Product Model
public function category()
{
return $this->belongTo(Category::class);
}
In Ctonroller
$products = Product::with('category')->paginate()
In Blade
#foreach($products as $product)
<tr class="product">
<td>{{ $product->product_name }}</td>
<td>${{ $product->price }}</td>
<td>{{ $product->category->name }}</td>
<td>{{ $product->product_description }}</td>
<td>Delete</button></td>
#endforeach
In your problem first you need to fetch all the associate category..
You can do it with eager loading .. https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
Then you need to check the category condition..
$products = App\PRODUCTMODELNAME::with('category')->get();
#foreach($products as $product)
<tr class="product">
<td>{{ $product->product_name }}</td>
<td>${{ $product->price }}</td>
<td>
#if($product->category->id === 1)
Sushi
#elseif($product->category->id === 1)
Drinks
</td>
<td>{{ $product->product_description }}</td>
<td>Delete</button></td>
#endforeach

Adding columns in table

I have display quantity and price attributes on my table from database but for some reasons I don't save the total into database but I display total by multiplying quantity * total on the table when data is being fetched.
Total for every column on my table is being displayed accurately. Is there a way that I can add all total columns on the table in my html?
PS: with my code, it only displays the total of the current column of the table
Table
<tbody>
#foreach($items as $items)
<tr>
<td>{{$item>id }}</td>
<td>{{$item->quantity}}</td>
<td>{{ $item->price}}</td>
<td>{{ $item->quantity * $item->price}}</td>
</tr>
#endforeach
<p>Sum: {{ $item->quantity * $item->price}}</p>
</tbody>
<tbody>
{{ $total = 0 }}
#foreach($items as $items)
<tr>
<td>{{$item>id }}</td>
<td>{{$item->quantity}}</td>
<td>{{ $item->price}}</td>
<td>{{ $item->quantity * $item->price}}</td>
{{ $total = $total + ($item->quantity * $item->price) }}
</tr>
#endforeach
<p>Sum: {{ $total }}</p>
</tbody>
You can use blade's #php directive like this
<tbody>
#php
$total = 0
#foreach($items as $items)
<tr>
<td>{{$item>id }}</td>
<td>{{$item->quantity}}</td>
<td>{{$item->price}}</td>
<td>{{$total = $total + ($item->quantity * $item->price)}}</td>
</tr>
#endforeach
<p>Sum: {{ $total }}</p>
#endphp
</tbody>
Try the following:
{{ $sum = 0 }}
<tbody>
#foreach($items as $items)
<tr>
<td>{{$item>id }}</td>
<td>{{$item->quantity}}</td>
<td>{{ $item->price}}</td>
<td>{{ $item->quantity * $item->price}}</td>
</tr>
$sum+ = $item->quantity * $item->price;
#endforeach
<p>Sum: {{ $sum }}</p>
</tbody>
I assume here $items is a Illuminate\Support\Collection. You can calculate a sum using a closure:
$items->sum( function ($item) {
return $item->quantity * $item->price;
});
Or in your blade template:
{{ $items->sum( function ($item) {
return $item->quantity * $item->price;
}); }}
See Laravel Collections docs.

Laravel total of products from Pivot table

#foreach($order->products as $product)
<tr>
<td> {{ $product->id }}</td>
<td> {{ $product->name }}</td>
<td> {{ $product->getRelationValue('pivot')->qty }} </td>
<td> {{ $product->getRelationValue('pivot')->price }} </td>
<td> {{ $total = $product->getRelationValue('pivot')->price * $product->getRelationValue('pivot')->qty }} </td>
</tr>
#endforeach
Grand Total:{{$total}}
I would like to show the total value. Can someone help me out?
And also the random access to $product->id in format 1,2,3....
You can do something like below code:
#php
$grnadTotal = 0;
#endphp
#foreach($order->products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->getRelationValue('pivot')->qty }}</td>
<td>{{ $product->getRelationValue('pivot')->price }}</td>
<td>{{ $total = $product->getRelationValue('pivot')->price * $product->getRelationValue('pivot')->qty }}</td>
</tr>
#php
$grnadTotal += $total;
#endphp
#endforeach
Grand Total:{{$grnadTotal}}
use this
Grand Total: {{$order->products->count()}}

Sum values in foreach loop in [php]

I want to sum tot_Price and unit_Price respectively so that I can analyze the average of these two attributes.
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
How should I do this?
Thanks.
You could add sum variable for both tot_Price and unit_Price and add in foreach loop
<?php $sum_tot_Price = 0 ?>
<?php $sum_unit_Price = 0 ?>
#foreach ($yourData as X_land)
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
<?php $sum_tot_Price += $X_land->tot_Price ?>
<?php $sum_unit_Price += $X_land->unit_Price ?>
#endforeach
<tr>
<td>Sum tot_Price {{ $sum_tot_Price}}</td>
<td>Sum unit_Price {{ $sum_unit_Price}}</td>
</tr>
Assume:
#foreach($lands as $X_land)
<tr>
<td>{{ $X_land->house_Type }}</td>
<td class="address">{{ $X_land->the_Location }}</td>
<td>{{ $X_land->tot_Price }}</td>
<td>{{ $X_land->tran_Area }}</td>
<td>{{ $X_land->unit_Price }}</td>
<td><button id="opener<?php echo $opencount?>">詳細資訊</button></td>
</tr>
#endforeach
You can get total like this:
{{$lands->sum('tot_Price')}}
{{$lands->sum('unit_Price')}}
Or you can get average:
{{$lands->avg('tot_Price')}}
{{$lands->avg('unit_Price')}}
To be very simple,
You need to focus on the #foreach loop first,
e.g #foreach($people as $person)
and there is the salary which you can access in foreach,
Through $person->salary
If you want to calculate the sum of salaries then use a simple sum like this,
$people->sum('salary'), Remember not use $person
In your case
{{$lands->sum('tot_Price')}}
{{$lands->sum('unit_Price')}}

Categories