Dynamic number of rows in Laravel Blade - php

I want a dynamic number of rows in a table like this.
number name
1 Devy
This my Blade template.
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $value)
<tr>
<td></td>
<td>{{$value->name}}</td>
</tr>
#endforeach
</tbody>
How do I do that?

Try $loop->iteration variable.
`
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $value)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>
`

This is correct:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
And you must use index+1 because index starts from 0.
Using raw PHP in view is not the best solution. Example:
<tbody>
<?php $i=1; #foreach ($aaa as $value)?>
<tr>
<td><?php echo $i;?></td>
<td><?php {{$value->name}};?></td>
</tr>
<?php $i++;?>
<?php #endforeach ?>
in your case:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $index => $value)
<tr>
<td>{{$index}}</td> // index +1 to begin from 1
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>

Use a counter and increment its value in loop:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
<?php $i = 0 ?>
#foreach ($aaa as $value)
<?php $i++ ?>
<tr>
<td>{{ $i}}</td>
<td>{{$value->name}}</td>
</tr>
#endforeach
</tbody>

Use $loop variable
refer this link Loop Variable

Starting from Laravel 5.3, this has been become a lot easier. Just use the $loop object from within a given loop. You can access $loop->index or $loop->iteration. Check this answer: https://laracasts.com/discuss/channels/laravel/count-in-a-blade-foreach-loop-is-there-a-better-way/replies/305861

Just take a variable before foreach() like $i=1. And increment $i just before foreach() ends. Thus you can echo $i in the desired <td></td>

try the following:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $index => $value)
<tr>
<td>{{$index}}</td>
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>

You can Use it like this in Blade. I hope this will help.
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $index => $value)
<tr>
<td>{{$index +1}}</td>
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>
Note: {{$index +1}} since $index starts from 0

Laravel 8 -
<?php $serial = "1"; ?> - Before #foreach loop
<td>{{ $serial++ }}</td> - Inside #foreach loop

Related

Show two array values inside one foreach loop in Laravel

I'm retrieving the JSON array from my DB and my array is:
{"Position":["first","second","third","fourth"],"Color":["Red",Blue,Pink,Teal]}
I want to show them in table inside a foreach loop
#foreach ($orderProduct->column as $item)
<thead>
<tr>
<th>{{ $item['Position'] }}</th>
<th>{{ $item['Color'] }}</th>
</tr>
</thead>
#endforeach
I want the output to be like this
<thead>
<tr>
<th>first</th>
<th>Red</th>
</tr>
</thead>
<thead>
<tr>
<th>second</th>
<th>Blue</th>
</tr>
</thead>
<thead>
<tr>
<th>third</th>
<th>Pink</th>
</tr>
</thead>
But it doesn't work.
Any ideas?
#for ($i = 0; $i < count($orderProduct->column['Position']); $i++)
<thead>
<tr>
<th>{{ $orderProduct->column['Position'][$i] }}</th>
<th>{{ $orderProduct->column['Color'][$i] }}</th>
</tr>
</thead>
#endfor
#foreach (array_combine($orderProduct['Position'], $orderProduct['Color']) as $position => $color)
<thead>
<tr>
<th>{{ $position }}</th>
<th>{{ $color }}</th>
</tr>
</thead>
#endforeach
Because you're using Laravel, I would suggest to make use of the Collection's zip method that "merges together the values of the given array with the values of the original collection at their corresponding index" (Docs)
You would use it like this:
$newArray = collect($array['Position'])->zip($array['Color']);
#foreach($newArray as [$position, $color])
{{ $position }}
{{ $color }}
#endforeach

How to get total / sum before first row in php

Please help me, I need output total on top of data shown...
I have Looping like this in my blade.php
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
and output will like this:
And now how to get output total before firs row like image below?
The laravel collection has an inbuilt function called sum which returns the sum of Collection.
Learn here about collection function
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Total Pencil</td>
<td>{{ $datas->sum('amount') }}</td>
</tr>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
convert object to array
$datasArray = json_decode(json_encode($datas), true);
collect all 'amount' in an array
$amountArray = array_column($datasArray,'amount');
Sum all element of $amountArray
$total = array_sum($amountArray );
print $total where you want

Print Array to table

I have an array with data and I have to put them into a table.
Array:
array(
0=>1
1=>Jon
2=>jon#email.com
3=>2
4=>Doe
5=>doe#email.com
6=>3
7=>Foo
8=>foo#email.com)
So Table head is:
<table>
<head>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
</table>
If I go and loop through the array:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach($array as $data)
<tr>
<td>
{{$data}}
</td>
</tr>
#endforeach
<tbody>
</table>
Current Output:
Id Name Email
1 Jon jon#email.com 2 Doe doe#email.com 3 Foo foo#email.com
But My desired output is:
Id Name Email
1 Jon jon#email.com
2 Doe doe#email.com
3 Foo foo#email.com
I tried to chunk array into arrays with array_chunk() but the result was the same.
I am on laravel but I can go with plain php also.
Any tips would be much apreciated.
<tbody>
#foreach(array_chunk($array, 3) as $data)
<tr>
<td>
{{ $data[0] }}
</td>
<td>
{{ $data[1] }}
</td>
<td>
{{ $data[2] }}
</td>
</tr>
#endforeach
<tbody>
Hope this will help you.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#for($i = 0; $i < count($array); $i= $i+3)
<tr>
<td>
{{ $array[$i] }}
</td>
<td>
{{ $array[$i + 1] }}
</td>
<td>
{{ $array[$i + 2] }}
</td>
</tr>
#endforeach
<tbody>
</table>
First of all no way your current code will give that output you're showing. It should be vertical, each element in its separate row. But let's fix the problem.
You don't have proper objects to work on so here condition is that after 3 iterations, new data is coming. So you can take mod of 3 and make it work like this:
#foreach ( $array as $key => $value )
// you have a new dataset
#if ( ($key % 3) == 0 )
<tr>
#endif
<td>{{ $value }}</td>
#if ( ($key % 3) == 0 )
</tr>
#endif
#endforeach
I have not tested this code above but it should work. Though I would always prefer to use proper List of objects of User class or something else.
EDIT: I would prefer #Sand Of Vega's answer. Its better.
Try This
Re-place your with following.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($array as $key => $value)
if($key % 3 == 0){
echo "</tr>";
}
echo "<td>".$value."</td>";
#endforeach
<tbody>
</table>
I am not so familiar with laravel butI hope following works for you.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
$length = (int) count($array)/3;
#for($i = 0; $i < $length; $i+=3)
<tr>
<td>{{$data[$i]}}</td>
<td>{{$data[$i+1]}}</td>
<td>{{$data[$i+2]}}</td>
</tr>
#endfor
<tbody>
</table>

Laravel, what is the best/alternative way to check if record exists and display them within table?

Currently im using Laravel-Excel (MaatWebsite) and wanted to check whether excel data is same with data in database "furnitures" table
Controller
// assume $id is retrieve from parameter
$check_furnitures = Furniture::where('company_no', $id)->get();
$rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader){$reader->noHeading();$reader->skipRows(1)})->get();
View
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php $arr = array(); ?>
#foreach($rows as $key => $value)
#foreach($check_furnitures as $fur)
#if(
$fur->chairs == $value[1] &&
$fur->desks == $value[2] &&
$fur->carpets == $pvalue[3]
)
<?php $temp_array[] = $value[0] ?>
#endif
#endforeach
#endforeach
<tr>
<td>{{$value[1]}}</td>
<td>{{$value[2]}}</td>
<td>{{$value[3]}}</td>
<td>
#foreach($arr as $test)
#if($test == $value[0])
DUPLICATED VALUE
#endif
#endforeach
</td>
<tr>
<table>
Excel File
The most efficient way is to index your database results first instead of looping. Laravel has a $collection->keyBy('id') so that you can $collection->get($id) later.
Having said that, you can do it this way:
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
#php
$arr = array();
$check_furnitures->keyBy('number');
#endphp
#foreach ($rows as $key => $row)
<tr>
<td>{{ $row->chair }}</td>
<td>{{ $row->desk }}</td>
<td>{{ $row->table }}</td>
<td>
#if($check_furnitures->get($row->number) !== null)
DUPLICATED VALUE
#endif
</td>
</tr>
#endforeach
</tbody>
</table>

How to include all the data in foreach loop inside a table

#foreach($lastactivity as $activity)
{{ $activity }}
#endforeach
That is the for loop.
Now this is the output in the view.
{"id":2,"log_name":"index-log","description":"I visited index","subject_id":null,"subject_type":null,"causer_id":1,"causer_type":"App\\User","properties":[],"created_at":"2017-07-18 11:05:08","updated_at":"2017-07-18 11:05:08"}
How will I be able to put that in a table with all the columns and data arranged? Thank you very much.
You should use table.
<table>
<thead>
<tr>
<th>Log Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($lastactivity as $activity)
<tr>
<td>{{$activity->log_name}}</td>
<td>{{$activity->description}}</td>
</tr>
#endforeach
</tbody>
</table>
In the same way, Add other fields as well.
You have to define your field name
{{ $activity->id }}
{{ $activity->log_name }}
etc
Like this:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Log Name</th>
.
.
.
<th>Updated At</th>
</tr>
</thead>
<tbody>
#foreach($lastactivity as $activity)
<tr>
<td>{{ $activity->id }}</td>
<td>{{ $activity->log_name }}</td>
.
.
.
<td>{{ $activity-> }}</td>
</tr>
#endforeach
</tbody>
</table>

Categories