Laravel total of products from Pivot table - php

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

Related

I need to get value from input with id of selected item and return them to controller

I am new to Laravel and I am trying to get value of input with selected element's id.
When I am trying to get input value of quantity in controller it's actually empty.
Here is my code.
collect.blade.php
#foreach($reagentcard as $r)
<tr>
<td>{{ $i++ }}</td>
<td>{{ $r->casnumber }}</td>
<td>{{ $r->name }}</td>
<td>{{ $r->description }}</td>
<td>{{ $r->classification }}</td>
<td>{{ $r->manname }}</td>
<td>{{ $r->incomedate}} </td>
<td>{{ $r->expirationdate}} </td>
<td>{{ $r->quantity }} </td>
<td><input class="form-control" type="number" name="quantity"></td>
#if($message = Session::get('info'))
<strong style="color:red">{{ $message }}</strong><br>
#endif
#if($message = Session::get('error'))
<strong style="color:red">{{ $message }}</strong><br>
#endif
<td><button> Collect </button> </td>
#endforeach
controller.php
public function collectReagent($id, Request $request)
{
$user_id = Auth::user()->id;
$takedate = Carbon::now();
$reagentcard = ReagentCard::where('id',$id)->first();
$test = $request->route('quantity');
return view('test',compact('test', 'id'));
}

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

Foreach loop (laravel blade)

I need help with a foreach loop in laravel. For some reason I need to have two rows instead of two tables (would have solved my problem) However, I have problems with it getting to work due to not knowing where to place my tags to get the right table structure.
Code:
<table class="minimalistBlack">
<tbody>
<tr>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
<th>X</th>
<th>Etternavn</th>
<th>Navn</th>
<th>Posthylle</th>
</tr>
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
#foreach ($data2 as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
#endforeach
</tbody>
</table>
How can I combine the two foreach loops and and get the right table structure?
to achieve just that result you could do
foreach (array_combine($courses, $sections) as $course => $section)
but that only works for two arrays
or u can use two table and make feel as single table
<div class="row">
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
#foreach ($data as $mailbox)
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
#endforeach
</table>
</div>
Assuming the $data and $data2 variables are Laravel collection instances, you can simply use $data = $data->concat($data2). Now $data will contain data of both variables. For a simple array, you can use $data += $data2;. This will merge $data & $data2 to $data. Then you can simply use:
...
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach
</tbody>
</table>
User array_merge function in controller like this
$array1 = array("john","ny");
$array2 = array("mitchel","ny");
$result = array_merge($a1,$a2);
if $data and $data2 have one, one row you can try like this.
#foreach ($data as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
<td></td>
</tr>
#endforeach
#foreach ($data2 as $mailbox)
<tr>
<td>{{ $mailbox->last_name }}</td>
<td>{{ $mailbox->first_name }}</td>
<td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
#endforeach

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

Laravel function to blade

I want a help to change this function of a model in my project cause it returns an array like this :: [{"Package_Name":"Excel"}] i need only the string "EXCEL"
Code in model
public function getUserSeminars(){
$seminar = Seminar::select('Package_Name')
->join('Home_StudentPackages','Home_StudentPackages.hsp_PackID','=','Home_Packages.Package_ID')
->join('Home_Students','Home_Students.home_id','=','Home_StudentPackages.hsp_homeStudID')
->where('Home_Students.home_id','=',$this->home_id)
->get();
return $seminar;
}
blade
#foreach($users as $i=>$user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->home_lastname }}</td>
<td>{{ $user->home_firstname }} </td>
<td>{{ $user->getUserSeminars() }}</td>
<td>{{ $user->home_id }}</td>
<td>{{ $user->getUserRights() }}</td>
#foreach($users as $i=>$user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->home_lastname }}</td>
<td>{{ $user->home_firstname }} </td>
<td>
<ul>
#foreach($user->getUserSeminars() as $seminar)
<li>{{$seminar->Package_Name}}</li>
#endforeach
</ul>
</td>
<td>{{ $user->getUserSeminars() }}</td>
<td>{{ $user->home_id }}</td>
<td>{{ $user->getUserRights() }}</td>
Kind of some pseudo code, can't remember 100% correct sintaxis, but this should work.

Categories