John Davidson

php - how to find the rows of my table in which the value of remaining field is more than zero in laravel

0 comments
Message:


i have a layout that shows all the orders placed in which i have record of paid and remaining amount also , so i am trying to display the background color of rows as red if remaining field data is more than 0 for this i am trying this method


public function noBalance() {

return Order::where('remaining', '>', 0)->first();
}

i am creating this in model file of Order
also tried this


return Order::where('remaining', '>', 0);

and


  @foreach ($orders as $order)

<tr style="{{ $order->noBalance() ? 'background-color: lightcoral;' : '' }}">
(here i am using that function in my allorder.blade.php)
<td>{{$order->id}}</td>
<td>{{$order->client}}</td>
<td>{{$order->salesmanRelation->employee_name}}</td>
<td>{{$order->orderBooker->employee_name}}</td>
<td>{{$order->total_amount}}</td>
<td>{{$order->paid}}</td>
<td>{{$order->remaining}}</td>
<td>{{$order->discount}}</td>
<td>{{$order->created_at->toFormattedDateString()}}</td>
<td><a href="/orders/{{$order->id}}">Detail</a></td>
<td><a href="/orders/{{$order->id}}/edit">Edit</a></td>
</tr>

@endforeach

but after using this all the rows of my table gets light coral not only the rows with the remaining >0


please help!



My Answer:
You are close, but the issue is with your `noBalance()` method in the Order model. Instead of using `first()`, you should use `get()` to retrieve all the rows where the remaining field is greater than 0.

Here is the updated `noBalance()` method in your Order model:

php
public function noBalance() {
return Order::where('remaining', '>', 0)->get();
}


Then in your blade file, you can check if the current order is in the list returned by `noBalance()` method:

php
@foreach ($orders as $order)

{{$order->id}}
{{$order->client}}
{{$order->salesmanRelation->employee_name}}
{{$order->orderBooker->employee_name}}
{{$order->total_amount}}
{{$order->paid}}
{{$order->remaining}}
{{$order->discount}}
{{$order->created_at->toFormattedDateString()}}
Detail
Edit

@endforeach


This way, only the rows with remaining value greater than 0 will have a light coral background color.

Rate this post

3 of 5 based on 1087 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us