Foreach loop (laravel blade) - php

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

Related

Laravel sort two foreach loop

I have two foreach loops:
#foreach($masseurs as $masseur)
<tr>
<td>{{ $masseur->name }}</td>
<td>{{ $masseur->visa_expire }}</td>
</tr>
#endforeach
#foreach($masseurs as $masseur)
<tr>
<td>{{ $masseur->name }}</td>
<td>{{ $masseur->passsport_expire }}</td>
</tr>
#endforeach
I would like to sort the results of the two loops with sortBy, so I assume that I need to combine them. How to do that?
In Controller:
$masseurs->sortBy('visa_expire');
In view:
#foreach($masseurs as $masseur)
<tr>
<td>{{ $masseur->name }}</td>
<td>{{ $masseur->visa_expire }}</td>
<td>{{ $masseur->passsport_expire }}</td>
</tr>
#endforeach

Trying to access to array in view return Trying to get property of non-object

I'm using Laravel 5.2, I'm trying to execute a stored procedure and display the result in a view using a foreach loop. When I run my app I get the following error message:
Trying to get property of non-object
If I use return or dump I get the following result:
Here is my controller code:
public function VentasPorSucursal() {
$values = [1, 25, 2, "NULL", "2018-08-10", "2018-08-10", "NULL", "NULL"];
$data['data'] = DB::select('SP_RPT_VENTAS2 ?, ?, ?, ?, ?, ?, ?, ?', $values);
//dump($data);
return view('reportes.index')->with('data', $data);
}
And this is my index.blade.php
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed table-hover">
<thead>
<th>Folio</th>
<th>Fecha</th>
<th>Sucursal</th>
<th>Vendedor</th>
<th>Cantidad</th>
<th>Unidad</th>
<th>Descripción</th>
<th>Pzaz x Pq</th>
<th>Precio</th>
<th>Total</th>
<th>Total Pzaz. Vendidas</th>
</thead>
#foreach ($data as $result)
<tr>
<td>{{ $data->Folio }}</td>
<td>{{ $data->Fecha }}</td>
<td>{{ $data->Sucursal }}</td>
<td>{{ $data->Vendedor }}</td>
<td>{{ $data->Cantidad }}</td>
<td>{{ $data->Unidad }}</td>
<td>{{ $data->Descripcion }}</td>
<td>{{ $data->PzasxPq }}</td>
<td>{{ $data->Precio }}</td>
<td>{{ $data->Total }}</td>
<td>{{ $data->TotalPzasVendidas }}</td>
</tr>
#endforeach
</table>
</div>
{{$data->render()}}
Looking for an answer I found that using '->' it's for an array and maybe my code is returning the result as an object and I need to use '[]' but I don't know exactly how can I do it.
What could be the problem?
Looking at your dump result. The data array you are passing to view contains a data key which contains all of your objects returned from query. So you can loop through data variable data objects like this:
#foreach ($data['data'] as $result)
<tr>
<td>{{ $result->Folio }}</td>
<td>{{ $result->Fecha }}</td>
<td>{{ $result->Sucursal }}</td>
<td>{{ $result->Vendedor }}</td>
<td>{{ $result->Cantidad }}</td>
<td>{{ $result->Unidad }}</td>
<td>{{ $result->Descripcion }}</td>
<td>{{ $result->PzasxPq }}</td>
<td>{{ $result->Precio }}</td>
<td>{{ $result->Total }}</td>
<td>{{ $result->TotalPzasVendidas }}</td>
</tr>
#endforeach
Your first problem is in foreach loop which i reset is below and secondly
If the return result is an array it can be access like below.
#foreach ($data as $result)
<tr>
<td>{{ $result["Folio"]}}</td>
<td>{{ $result["Fecha"] }}</td>
<td>{{ $result["Sucursal"] }}</td>
<td>{{ $result["Vendedor"] }}</td>
<td>{{ $result["Cantidad"] }}</td>
<td>{{ $result["Unidad"] }}</td>
<td>{{ $result["Descripcion"] }}</td>
<td>{{ $result["PzasxPq"] }}</td>
<td>{{ $result["Precio"] }}</td>
<td>{{ $result["Total"] }}</td>
<td>{{ $result["TotalPzasVendidas"] }}</td>
</tr>
#endforeach

Illegal string offset while trying print the value with in the array

I am rendering view in mail template but in my view I am getting following error when I use dd($row['product_name']);. I get product name but not in following code don't know meaning of this error:
#foreach ($order as $row)
<tr>
<td>{{ $row['product_name'] }}</td>
<td>{{ $row['amount'] }}</td>
<td>{{ $row['quantity'] }}</td>
</tr>
#endforeach
getting error:
Illegal string offset 'product_name'
$order is an object $row->product_name and not an array $row['product_name'].
#foreach ($order as $row)
<tr>
<td>{{ $row->product_name }}</td>
<td>{{ $row->amount }}</td>
<td>{{ $row->quantity }}</td>
</tr>
#endforeach
It might be because of 'product_name' key is not exist in your array element. Before using it, check is that key exist in that array element with isset
#foreach ($order as $row)
<tr>
<td>{{ isset($row['product_name'])?$row['product_name']:'' }}</td>
</tr> #endforeach
Try this one:
#foreach ($order as $row)
<tr>
<td>{{ $row->product_name }}</td>
<td>{{ $row->amount }}</td>
<td>{{ $row->quantity }}</td>
</tr>
#endforeach
Check out the laravel documentation for blade https://laravel.com/docs/5.6/blade
Can you try this solution
#foreach ($order as $row)
<tr>
<td>{{ $row->product_name }}</td>
<td>{{ $row->amount }}</td>
<td>{{ $row->quantity }}</td>
</tr>
#endforeach
add dd($row) insted of dd($row['product_name']).
Your Controller returns Object not Array so you try like that
#foreach ($order as $row)
<tr>
<td>{{ $row->product_name }}</td>
<td>{{ $row->amount }}</td>
<td>{{ $row->quantity }}</td>
</tr>
#endforeach
$order is an object you can not use it like an array
#foreach ($order as $row)
<tr>
<td>{{ $row->product_name }}</td>
<td>{{ $row->amount }}</td>
<td>{{ $row->quantity }}</td>
</tr>
#endforeach
If you want to to use the record like this then you need to convert the object toArray()
$order = $order->toArray();
After that you can use like this: $row['product_name']
here $order is an object so you cant use like $row['product_name'].you can use the property of an object using -> operator. so try like this
#foreach ($order as $row)
<tr>
<td>{{ $row->product_name }}</td>
<td>{{ $row->amount }}</td>
<td>{{ $row->quantity }}</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