foreach loop inside html table design in laravel - laravel

I have an array of data to display using table in html .but the foreach loop which i m using is not giving the desired format .
Below is the array data
$data = Array
(
[0] => Array
(
[id] => 10
[asset_name] => Mini Paver
[qty] => 3
[est_start_date] => 02/05/2017
[days] => 2
[comments] => Comment 2
[bundle_name] => 1XRoller 1XPaver
)
[1] => Array
(
[id] => 11
[asset_name] => Roller
[qty] => 2
[est_start_date] => 03/07/2018
[days] => 4
[comments] => Comment 2
[bundle_name] => 1XRoller 1XPaver
)
)
my view html code :
#foreach($data as $value)
<table class="" style="width: 100%;border:1px solid #ccc">
<thead>
<tr>
<th colspan="4"> <p><?php echo $value['bundle_name'];?> </p></th>
</tr>
<tr>
<th style="text-align: center">id</th>
<th style="width:5%;text-align: center">Asset Category</th>
<th style="width:5%;text-align: center">Days</th>
<th style="width:5%;text-align: center">Qty</th>
</tr>
</thead>
<tbody>
<tr>
<th style="text-align: center"><?php echo $value['id'];?> </th>
<th style="width:5%;text-align: center"><?php echo $value['asset_name'];?></th>
<th style="width:5%;text-align: center"><?php echo $value['days'];?></th>
<th style="width:5%;text-align: center"><?php echo $value['qty'];?></th>
</tr>
</tbody>
</table>
#endforeach
By using above for each loop i m getting the below html format like bundle name is repeating .
But i need the output should be like as below :
that means i want the bundle name shluld come only one time and the rest of details should display as in rows .
How do i do that ? any suggestions please ?
Thank you .

Please try the below code,
I have assumed the below points,
1. Your bundle_name will change(that is you will have various bundle_name). This code will work even if you have single bundle_name.
2. You will sort the result by bundle_name.
3. You need bundle title & table header for each bundle_name(again, this code will work even if you have single bundle_name).
4. bundle_name will never have a value false.
#php ($bundle_name = false)
#foreach($data as $value)
#if($bundle_name != $value['bundle_name'])
#if($bundle_name != false)
</tbody>
</table>
#endif
#php ($bundle_name = $value['bundle_name'])
<table class="" style="width: 100%;border:1px solid #ccc">
<thead>
<tr>
<th colspan="4"> <p> {{ $bundle_name }} </p></th>
</tr>
<tr>
<th style="text-align: center">id</th>
<th style="width:5%;text-align: center">Asset Category</th>
<th style="width:5%;text-align: center">Days</th>
<th style="width:5%;text-align: center">Qty</th>
</tr>
</thead>
<tbody>
#endif
<tr>
<th style="text-align: center">{{ $value['id'] }} </th>
<th style="width:5%;text-align: center">{{ $value['asset_name'] }}</th>
<th style="width:5%;text-align: center">{{ $value['days'] }}</th>
<th style="width:5%;text-align: center">{{ $value['qty'] }}</th>
</tr>
#endforeach
</tbody>
</table>

Related

laravel 9 Update, change a field of all rows with the same reference

I ask if anyone can help me, I import an excel table and create several rows of data with a column with the same reference, I want to update a field with a single input of all the rows with the 'Same' reference, for example I want to change the value of the same column with a single input and update the data to all, or add if it does not exist
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Same</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>255</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>255</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>255</td>
</tr>
</tbody>
</table>

Laravel Livewire checkbox checked on edit page

I'm facing an issue with checking the checkbox on the edit page using Livewire. I've tried many ways but still not able to check the old selected value
View Code:
<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds.{{$property->id}}"
value="{{$property->id}}" #if(in_array($property->id,$propertyIds)) checked #endif>
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>
Backend Code:
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}```
updated
And can you check if this correct? wire:model="propertyIds.{{$property->id}}"? Try to delete the wire:model .
<input class="branchCheckbox" type="checkbox"
value="{{$property->id}}"
#if(in_array($property->id,$propertyIds)) checked #endif>
`<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds"
value="{{$property->id}}" >
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>`
BACKEND
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}

Calculate difference in minutes between created_at and current time in Laravel

I want to show in a field the time someone is waiting, and I want to do that by subtracting created_at time by current time in minutes
this is my table
<thead>
<tr>
<th scope="col">Ordernummer</th>
<th scope="col">E-mail</th>
<th scope="col">Kenteken</th>
<th scope="col">Wachttijd</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
#foreach($orders as $order)
<tr>
<th scope="row">{{ $order->ordernumber }}</th>
<td scope="row">{{ $order->email }}</td>
<th scope="row">{{ $order->numberplate }}</th>
<td scope="row">{{ $order->created_at }}</td>
<td scope="row">
#if( $order->status == 0)
Open
#else
Afgehandeld
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
And where this is written <td scope="row">{{ $order->created_at }}</td>
I want to show there the difference in minutes.
Thanks already
I assume your $order->created_at is Carbon instance, so you can use:
<td scope="row">{{ $order->created_at->diffInMinutes(\Carbon\Carbon::now()) }} minutes ago</td>
if $order->created_at is pure mysql format not Carbon instance just convert/create it:
\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $order->created_at)->diffInMinutes(\Carbon\Carbon::now())
You can use Carbon to calculate difference in minutes like so
<td scope="row">{{ now()->diffInMinutes($order->created_at) }}</td>
you can use Carbon::diffInMinutes to get the minutes differnt,
or you can use Carbon::diffForHumans to get more readable statement
$order->created_at->diffInMinutes(Carbon::now());
// that would be: 5
$order->created_at->diffInMinutes(Carbon::now());
and this would be: 5 minuets ago

How to Display the results in a table format [duplicate]

This question already has answers here:
Horizontal table layout using CSS
(4 answers)
Closed 2 years ago.
I want to display the result of the permissions table in a table format... I'm using Laratrust for roles and permissions
my RoleController
public function show($id)
{
$role = Role::with('permissions')->with('users')->where('id', $id)->first();
return view('admin.roles.show', compact('role'));
}
I want the results to be display in this table horizontally like the way I hard-coded it
show.blade.php
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">PERMISSION</th>
<th scope="col">CREATE</th>
<th scope="col">READ</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="col">Users</th>
<td>Create User</td>
<td>Read User</td>
<td>Update User</td>
<td>Delete User</td>
</tr>
<tr>
<th scope="col">Profile</th>
<td>Create Profile</td>
<td>Read Profile</td>
<td>Update Profile</td>
<td>Delete Profile</td>
</tr>
</tbody>
</table>
<thead class="thead-dark">
<tr>
<th scope="col">PERMISSION</th>
<th scope="col">CREATE</th>
<th scope="col">READ</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($role as $key => $data)
<tr>
<th>{{$data->permission}}</th>
<th>{{$data->create}}</th>
<th>{{$data->read}}</th>
<th>{{$data->update}}</th>
<th>{{$data->delete}}</th>
</tr>
#endforeach
</tbody>
I think this should work if role variable have all the data
You can read in Laravel docs for more information https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
And make sure in your Role model has permissions, users relationship then try this:
// controller file
public function show($id)
{
$role = Role::with(['permissions', 'users'])->where('id', $id)->first();
return view('admin.roles.show', compact('role'));
}
// blade file
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">PERMISSION</th>
<th scope="col">CREATE</th>
<th scope="col">READ</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($role->permissions as $permission)
<tr>
<th scope="col">{{ $permission->name }}</th>
<td>Create User</td>
<td>Read User</td>
<td>Update User</td>
<td>Delete User</td>
</tr>
#endforeach
</tbody>
</table>

How can I create dynamic table in Thymeleaf..?

I am new to Thymeleaf and trying to create a dynamic table on Themeleaf template.
How can I do it..??
I have been googling by I didn't got any proper answer. The issue is I cannot iterate List< Map< String,Object >>. I can have any number of columns and columns name could be any thing.
<tr class="headings">
<th class="column-title">ID</th>
<th class="column-title">Name</th>
<th class="column-title">Salary</th>
<th class="column-title">Status</th>
</tr>
</thead>
<tbody>
<tr class="even pointer" th:each="row:${rows}" id = "tablerow">
<td class=" " th:text="${row.getId()}">Id</td>
<td class=" " th:text="${row.getName()}">Name</td>
<td class=" " th:utext="${row.getSalary()}">Salary</td>
<td class=" " th:text="${row.getStatus()}">Active</td>
</tr>
</tbody>
I need to set values dynamically since if query of result will keep changing . right now column name are hard coded and value are also getting by row.getId what if there is no Id, it could be anything in rows what shall I use than..? example row.<>.
rows is obtained as List< Map< String, Object>>.
Thanks in advance.
You can iterative over a Map just as easily as you can a List. The simplest form of this would be:
<tbody>
<tr class="even pointer" th:each="row: ${rows}" id="tablerow">
<td th:each="field: ${row}" th:text="${field.value}" />
</tr>
</tbody>
However, since Maps don't have a specific ordering (unless you're using something like a TreeMap), the way I would do it would be something like this (complete example should match your example table):
Controller
List<String> headers = Arrays.asList("ID", "Name", "Salary", "Status");
List<Map<String, Object>> rows = new ArrayList<>();
rows.add(Map.of("ID", "1", "Name", "Jim", "Salary", "50000", "Status", "active"));
rows.add(Map.of("ID", "2", "Name", "Sally", "Salary", "50000", "Status", "inactive"));
Template
<table>
<thead>
<tr class="headings">
<th th:each="header: ${headers}" class="column-title" th:text="${header}" />
</tr>
</thead>
<tbody>
<tr class="even pointer" th:each="row: ${rows}" id="tablerow">
<td th:each="header: ${headers}" th:text="${row.get(header)}" />
</tr>
</tbody>
</table>
Which will produce:
<table>
<thead>
<tr class="headings">
<th class="column-title" >ID</th>
<th class="column-title" >Name</th>
<th class="column-title" >Salary</th>
<th class="column-title" >Status</th>
</tr>
</thead>
<tbody>
<tr class="even pointer" id="tablerow">
<td >1</td>
<td >Jim</td>
<td >50000</td>
<td >active</td>
</tr>
<tr class="even pointer" id="tablerow">
<td >2</td>
<td >Sally</td>
<td >50000</td>
<td >inactive</td>
</tr>
</tbody>
</table>

Resources