Adding columns in table - php

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.

Related

Laravel view foreach with two properties

i need help with my foreach loop in view
Controller.php
$expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id');
$categories = Category::all();
return view('dashboard.index', compact('expenses','categories'));
index.php
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
<td>{{ $expense }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I get the output like this.
enter image description here
It outputs the expense 2 times, is there a limiter for "foreach loop" or is there another way on doing this?
You need to add a condition for that $category->id is the same as category_id
#foreach($expenses as $key => $expense)
#if($key == $category->id)
<td>{{ $expense }}</td>
#endif
#endforeach
The right way to do it is by using join or relationship.
$expenses = Expense::join('categories','categories.category_id','expenses.category_id')->groupBy('expenses.category_id')->selectRaw('sum(expenses.amount) as sum, expenses.category_id','categories.name')->get();
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($expenses as $expense)
<th>{{ $expense->name }}</th>
<td>{{ $expense->sum }}</td>
#endforeach
</tbody>
</table>
You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty
#foreach ($element['subs'] as $item)
#if($loop->index < 10)
// Your code
#endif
#endforeach
You should compare category_id with category->id.
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
#if($expense->category_id == $category->id )
<td>{{ $expense->sum }}</td>
#endif
#endforeach
</tr>
#endforeach

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

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()}}

How to display all row in database - Laravel 5.2?

I have simple issue with my Laravel application.But i can not find any solution of it.Please see my code here:
controller:
public function index(){
$members = Member::paginate(15);
$meal_entry = Member::all();
$breakfast = DB::SELECT("SELECT self_meal_breakfast + guest_meal_breakfast AS breakfast FROM meal_entry");
$lunch = DB::SELECT("SELECT self_meal_lunch + guest_meal_lunch AS lunch FROM meal_entry");
$dinner = DB::SELECT("SELECT self_meal_dinner + guest_meal_dinner AS dinner FROM meal_entry");
$total_meal = DB::SELECT("SELECT self_meal_breakfast + self_meal_lunch + self_meal_dinner + guest_meal_breakfast + guest_meal_lunch + guest_meal_dinner AS total FROM meal_entry");
return view('member.mealView', compact('members','data','breakfast','lunch','dinner','meal_entry','total_meal'));
}
blade view:
<tbody>
<tr>
#foreach($meal_entry as $value)
<td>{{ $value->created_at }}</td>
#endforeach
#foreach($breakfast as $value)
<td>{{ $value->breakfast }}</td>
#endforeach
#foreach($lunch as $value)
<td>{{ $value->lunch }}</td>
#endforeach
#foreach($dinner as $value)
<td>{{ $value->dinner }}</td>
#endforeach
#foreach($total_meal as $value)
<td>{{ $value->total }}</td>
#endforeach
#foreach($meal_entry as $value)
<td>{{ $value->created_at }}</td>
#endforeach
</tr>
</tbody>
And the table: meal_entry.
If there is a one row in the database then its ok ( see ) to view.But when there is row more than one then its show in one row with all data( see ).How can i handle this things? I think it should be run another loop but how can i do that? Thanks in advance.
So, if you're using Laravel, one of the best features that has is the accessors:
https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators
You can create new attributes and use them as model attributes.
For some stuff are very useful.
class Meal extends Model
{
public function getTotalBreakfastAttribute()
{
return $this->self_meal_breakfast + $this->guest_meal_breakfast;
}
}
And then, use this accessor as attribute:
<tbody>
#foreach($foods as $value)
<tr>
<td> {{$value->total_breakfast}} </td>
<td> {{$value->created_at}} </td>
</tr>
#endforeach
</tbody>
Or if you don't want to do that, instead of use multiple queries to get the data, just use one because all the fields are in the same table, and then, you could use the above example.
Enjoy! :)
A quick patch would be to do only one query and then do the calculations on the view (I know that is not a good practice to do the calculations in the view, but is the easiest way that i can see).
It would be like this:
Controller:
public function index(){
$members = Member::paginate(15);
return view('member.mealView', compact('members'));
}
blade view
<tbody>
#foreach($members as $meal_entry)
<tr>
<td>{{ $meal_entry->created_at }}</td>
<td>{{ $breakfast = $meal_entry->self_meal_breakfast + $meal_entry->guest_meal_breakfast }}</td>
<td>{{ $lunch = $meal_entry->self_meal_lunch+ $meal_entry->guest_meal_lunch}}</td>
<td>{{ $dinner = $meal_entry->self_meal_dinner + $meal_entry->guest_meal_dinner }}</td>
<td>{{ $breakfast + $lunch + $dinner }}</td>
<td>{{ $meal_entry->created_at }}</td>
</tr>
#endforeach
</tbody>
And remember to add
{{ $members->links() }}
Where you want the pagination links.

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