I have a problem with #foreach in my blade.php. When I insert movie with multiple categories my other moves to the right. Here is how it looks in browser
and here is code in blade
#if($movies)
<div class="row">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Actors</th>
</tr>
</thead>
<tbody>
#foreach($movies as $movie)
<tr>
<td>{{$movie->name}}</td>
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
#foreach($movie->actors as $actor)
<td>{{$actor->actor_name}}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
#endif
Your issue lies here:
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
You are saying that for each category, it should insert a new <td> into you table. This will of course skew the content since your table expects a set number of columns.
Instead, you should move your foreach statement inside the <td></td>
<td>
#foreach($movie->categories as $category)
{{$category->category_name}}
#endforeach
</td>
This will print the links for each category in the same column.
Related
I am new to using Laravel and I am currently trying to pass a variable from the index blade file to the controller in order to bring up all apparatus type fields associated with a specific apparatus code id.
I am able to access the apparatus type fields associated with the apparatus code id when I include a specific number which correlates to that apparatus code id (eg. inputting 2 brings up the apparatus type details for the apparatus code id 2, inputting 3 brings up the apparatus type details for the apparatus code id 3 etc).
However when I have tried to pass a variable relating to each individual apparatus code id using a for loop in the controller, the apparatus type loop appears to not be accessed and does not return any information. I will include snippets of my current code for clarity.
Any help or guidance with this issue would be greatly appreciated!
Controller file
public function index()
{
$apparatusCodes = ApparatusCodes::get();
foreach ($apparatusCodes as $apparatusCode){
$apparatusTypes = ApparatusTypes::where('apparatus_code_id', $apparatusCode->id)->get();
}
return view('apparatus_codes.index', compact('apparatusCodes', 'apparatusTypes'));
}
Index.blade file
<table id="datatable" class="stripe hover dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th>ID</th>
<th >Apparatus Code</th>
<th>Description</th>
<th>Rent</th>
<th></th>
</tr>
</thead>
<!--start of for loop-->
#foreach ($apparatusCodes as $apparatusCode)
<tbody>
<tr data-toggle="collapse" id="table{{ $apparatusCode->id}}" data-target=".table{{ $apparatusCode->id}}">
<td> {{ $apparatusCode->id}} </td>
<td>{{ $apparatusCode->apparatus_code}} </td>
<td> {{ $apparatusCode->description}}</td>
<td> {{ $apparatusCode->rent}}</td>
<td #click="isOpen = !isOpen" class="main-bg"><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/Icon-arrow-dropdown-white.png" alt="arrow down">
</td>
<td><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/edit-icon.svg" alt="Edit Icon">
</td>
</tr>
<tr x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak #click.away="isOpen = false" class="collapse table{{ $apparatusCode->id}}">
<td colspan="999">
<div>
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>Apparatus Code </th>
<th>Apparatus Type</th>
<th>Compensation </th>
<th>Created On </th>
<th>Created By </th>
<th></th>
#foreach ($apparatusTypes as $apparatusType)
</thead>
<tbody>
<tr>
<td>{{ $apparatusCode->apparatus_code}}</td>
<td>{{ $apparatusType->apparatus_type }}</td>
<td>{{ $apparatusType->compensation }}</td>
<td>{{ $apparatusType->created_at }}</td>
<td>{{ $apparatusType->users->name}}</td>
<td> <button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/Icon-edit-gray.png" alt="edit"></button></td>
</tr>
#endforeach
</table>
</tr>
</tr>
</td>
</tbody>
#endforeach
</table>
If you have the relationships setup you could eager load the ApparatusTypes for each ApparatusCode:
$apparatusCodes = ApparatusCodes::with('appartusTypes')->get();
Then in the view you could iterate the appartusTypes of each ApparatusCode:
#foreach ($apparatusCodes as $apparatusCode)
...
#foreach ($apparatusCode->apparatusTypes as $type)
...
#endforeach
...
#endforeach
Without using the relationships you could get all the ApparatusTypes for the ApparatusCodes and then group them by apparatus_code_id:
$codes = ApparatusCodes::get();
$types = ApparatusTypes::whereIn('apparatus_code_id', $codes->pluck('id'))
->get()
->groupBy('apparatus_code_id');
Then in the view you could iterate the group that corresponds to the current Code's apparatus_code_id:
#foreach ($codes as $code)
...
#foreach ($types->get($code->id, []) as $type)
...
#endforeach
...
#endforeach
Laravel 8.x Docs - Eloquent - Relationships - Eeager Loading with
Laravel 8.x Docs - Collections - Available Methods - pluck
Laravel 8.x Docs - Collections - Available Methods - groupBy
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi forum how to convert created_at column as header table and synchronized it with title column.
this is my database
below is my blade code
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
#foreach($users as $u=>$user)
#foreach($user->posts as $p)
<th>{{$p->created_at}}</th>
#endforeach
#endforeach
</tr>
</thead>
<tbody>
#foreach($users as $u=>$user)
<tr>
<td>{{$u+1}}</td>
<td>{{ $user->name }}</td>
#foreach($user->posts as $p)
<td>{{$p->title}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
this is my blade view
i have 2 table, user table and post table. a user could have many posts.
this is my post model
this is my user model
this my post controller
thank you.
You can collect all created_at in a new array with the help of #php directive and use 2 nested loops for comparison while displaying.
Run over all created_at and check if any of the post's timing matches. If yes, display the anchor tag and break out of the loop. If none of them matches, print an empty td.
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
#php
$created_ats = []
#endphp
#foreach($users as $u => $user)
#foreach($user->posts as $p)
<th>{{ $p->created_at }}</th>
#php
$created_ats[] = $p->created_at
#endphp
#endforeach
#endforeach
</tr>
</thead>
<tbody>
#foreach($users as $u => $user)
<tr>
<td>{{$u+1}}</td>
<td>{{ $user->name }}</td>
#foreach($created_ats as $created_at)
#foreach($user->posts as $p)
#if($p->created_at == $created_at)
<td>{{$p->title}}</td>
#break
#endif
#if($loop->last)
<td></td>
#endif
#endforeach
#endforeach
</tr>
#endforeach
</tbody>
</table>
</div>
If you want dates in sorted format, collect all of them in your controller, usort them with the help of DateTime class and pass them to the view.
Controller code:
<?php
public function index(){
$users = User:all();
$created_ats = [];
foreach($users as $u=>$user){
foreach($user->posts as $p){
$created_ats[] = $p->created_at;
}
}
usort($created_ats,function($date1,$date2){
$date1 = \DateTime::createFromFormat('Y-m-d H:i:s', $date1);
$date2 = \DateTime::createFromFormat('Y-m-d H:i:s', $date2);
return $date1 <=> $date2; // needs PHP7 minimum
});
return view('posts.index',compact('users','created_ats'));
}
Blade:
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
#foreach($created_ats as $created_at)
<th>{{ $created_at }}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($users as $u => $user)
<tr>
<td>{{$u+1}}</td>
<td>{{ $user->name }}</td>
#foreach($created_ats as $created_at)
#foreach($user->posts as $p)
#if($p->created_at == $created_at)
<td>{{$p->title}}</td>
#break
#endif
#if($loop->last)
<td></td>
#endif
#endforeach
#endforeach
</tr>
#endforeach
</tbody>
</table>
</div>
Am getting this error while generating pdf using DOMPDF
The row #2 could not be found, please file an issue in the tracker wit
I can view the output as html in my view page.But PDF is not generating am getting the above error.I have two arrays for the table headings and the results. MY code as follows:
My code:
<table width="100%" class="fullwidth border">
<thead>
<tr>
<th width="10%"></th>
<th width="10%"></th>
#foreach($selected_fields_top as $key => $value)
<th>{{ $value }}</th>
#endforeach
</tr>
</thead>
<tbody>
<table >
<tbody>
#foreach($selected_fields_left as $key => $value)
<tr><td><b>{{ $value }}</b></td>
<!-- <td> -->
#foreach($resultListLeft as $left_key => $valueArr)
#foreach($valueArr as $k => $v)
#if($v)
<table>
<tbody>
<!-- <tr> -->
#if($valueArr[$value])
<td width="10%">{{$valueArr[$value]}}</td>
#else
<td width="10%">Data not available</td>
#endif
#foreach($resultListTop as $key => $TopvalueArr)
#if($key==$left_key)
#foreach($TopvalueArr as $top_key => $top_value)
#if($top_value)
<td width="10%">{{$top_value}}</td>
#else
<td width="10%">Data not available</td>
#endif
#endforeach
#endif
#endforeach
<!-- </tr> -->
</tbody>
</table>
#endif
#endforeach
#endforeach
<!-- </td> -->
#endforeach
</tr>
</tbody>
</table>
i want to print the output like:-
Top heading1 Topheading2
Left heading 1 Left value Top value Top value
Left value Top value Top value
Left value Top value Top value
etc... etc... etc...
Left heading 2 Left value Top value Top value
Left value Top value Top value
Left value Top value Top value
etc... etc... etc...
First you have to var_dump the variable if they do contain such row.
I've been hunting around, but all the answers and documentation on this is for tables that aren't dynamically generating everything.
This is my blade file:
#extends('layout')
#section('content')
<h1>User Profile Data</h1>
<table class="table table-striped">
<tr>
#foreach ($columns as $column => $name)
<th><strong> {{ $name }} </strong></th>
#endforeach
#foreach ($profile as $person)
#foreach ($person as $name)
<td> {{ $name }} </td>
#endforeach
#endforeach
</tr>
</table>
#stop
It's all working, except for one problem, the heads go horizontally, rather than vertically. I'd like to be able to automatically populate the data from the database and have it displayed side by side.
Like so:
Name: John
Age: 25
Height: 176
etc.
What am I missing?
v.1 Directly generate the vertical headers table.
This can be easily solved after you realize how your table will actually look like in HTML, in order to have vertical headers.
<h1>User Profile Data</h1>
<table class="table table-striped">
<tr>
<th><strong> Name: </strong></th>
<td> John </td>
<td> Joane </td>
</tr>
<tr>
<th><strong> Surname: </strong></th>
<td> Doe </td>
<td> Donald </td>
</tr>
<tr>
<th><strong> Email: </strong></th>
<td> john.doe#email.com </td>
<td> jane#email.com </td>
</tr>
</table>
After you know your above goal you just have to nest the foreach loops so that you can populate the data.
For each column you'll have to return the matching data from the profile.
Instead of returning $person->name use $person[$col]
To nest the foreach use the same technique for your other question:
How to show data from (multidimensional?) arrays with Laravel 5.3 blade
v.2 Use normal tables combined with CSS tricks to reflow table
Import twitter bootstrap and use .table-reflow on a well formed table.
The generated table will look like this and can be generated with your initial laravel code:
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th ><strong> Name: </strong></th>
<th ><strong> Surname: </strong></th>
<th ><strong> Email: </strong></th>
</tr>
</thead>
<tbody>
<tr>
<td> John </td>
<td> Doe </td>
<td> john.doe#email.com </td>
</tr>
<tr>
<td> Joane </td>
<td> Donald </td>
<td> jane#email.com </td>
</tr>
</tbody>
</table>
Content order and complex tables:
Beware that the table-reflow style changes the visual order of content. Make sure that you only apply this style to well-formed and simple data tables (and in particular, don’t use this for layout tables) with appropriate <th> table header cells for each row and column.
In addition, this class will not work correctly for tables with cells that span multiple rows or columns (using rowspan or colspan attributes).
You should check the docs here:
twitter-bootstrap .table-reflow
Edit:
In addition to Kronmark's excellent points, I'm leaving this here for new people. This is what the automated table might look like:
<table class="table table-striped table-hover table-reflow">
#foreach($array as $key=>$value)
<tr>
<th ><strong> {{ $key }}: </strong></th>
<td> {{ $value }} </td>
</tr>
#endforeach
</table>
The key is the title from the table, the value is the data point associated with it. I.e. Full_Name = Key, John Magsson = value.
This is produced from an array, the process should be explained here:
Blade view not printing data from array
Good luck. :)
I am trying to get data from my table and show it in my view and paginate this data. I received this problem and couldn2t find any solution. I did the exactly same thing in my previous project and it worked well.
here is my Controller
public function hadiBirlikteCalisalimShow (){
$users =DB::table('birlikte_calisalim')->paginate(15);
return view('admin.birliktecalisalim',compact(['users']));
}
and this is my view
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Email</th>
<th>Tarih</th>
<th>İstek</th>
</tr>
</thead>
<tbody>
#foreach($users as $row)
<tr>
<td>{{$users->name}}</td>
<td>{{$users->email}}</td>
<td>{{$users->tarih}}</td>
<td>{{$users->message}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$users->links()}}
#foreach($users as $row)
<tr>
<td>{{$row->name}}</td>
<td>{{$row->email}}</td>
<td>{{$row->tarih}}</td>
<td>{{$row->message}}</td>
</tr>
#endforeach
should be $row->name,$row->email, etc... not $users->name, and change {{$users->links()}} to {!! $users->render() !!}