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>
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
I need to add two numbers passed by the foreach in the blade view
how can i add this two numbers instead of showing it as individual 333.34 333.34,
I need to show it as 666.68 / 1000 AUD
Here's how blade foreach looks like
#if(!empty($teams)&&count($teams)>0)
#foreach($teams as $key=>$team)
<table>
<tr>
<th>id</th>
<th>Team Name</th>
<th>Payment</th>
<th>Time Remaining</th>
<th>Num of Players Paid</th>
<th>Paid out of</th>
<th>Captain Name</th>
<th>Status</th>
</tr>
<tr>
<td>{{$key+1}}</td>
<td>{{$team->name}}</td>
<td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td>
<td>
#if ($team->pivot->status == 0)
{{\Carbon\Carbon::createFromTimeStamp(strtotime($team->pivot->created_at.' +1 day'))->diffForHumans(null, true, true, 2)}}
#else
Registered at {{$team->pivot->updated_at->todatestring()}}
#endif
</td>
<td>{{ $team->competitionPayments->count() . '/' . $team->players->count() .' PAID'}}</td>
<td>
#foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
#endforeach
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
<td>{{$team->captain->full_name}}</td>
<td>{{$team->pivot->status}}</td>
{{-- <td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td> --}}
</tr>
</table>
<table>
<tr>
<th>Full Name</th>
<th>DOB</th>
<th>Active Kids Voucher AKV</th>
<th>Accept Reject AKV</th>
<th>Paid $</th>
<th>Paid By </th>
<th>Total</th>
<th>Stripe</th>
<th>Mobile</th>
<th>Email</th>
<th>T&C</th>
</tr>
#foreach ($team->players as $player)
<tr>
<td>{{ $player->full_name }}</td>
<td>{{ $player->dob }}</td>
<td>-</td>
<td>-</td>
<td>
{!! $player->competitionPayments->isNotEmpty() ?
implode($player->competitionPayments->map(function($item, $key) {
return ($key > 0 ? '<div class="mb-1"></div>' : '') . number_format($item->amount, 2) . ' AUD <br> <hr>' . $item->updated_at->todatestring();
})->toArray()) : '-' !!}
</td>
<td>{{ $player->competitionPayments->isNotEmpty() ? $player->competitionPayments->implode('paidBy.name') : '-' }}</td>
<td>-</td>
<td>-</td>
<td>{{ $player->mobile }}</td>
<td>{{ $player->email }}</td>
<td>-</td>
</tr>
#endforeach
</table>
<br><br><hr><br><br>
#endforeach
#else
<tr>
<td colspan="6">
<div class="text-muted small text-center">
No teams have registered yet
</div>
</td>
</tr>
#endif
Heres the relationship in my Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
I tried to add the sum() and get the total as below and i got a wrong total and repeated
<td>
#foreach ($team->competitionPayments as $amount)
{{ $amount->sum('amount') }}
#endforeach
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
This is the total i got
Someone please help me out or suggest a way that i could fix this issue
Thank You
Try array_sum() method. Replace this block
#foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
#endforeach
With this
#php
$temparr = (array) $team->competitionPayments
#endphp
{{array_sum(array_column($temparr,'amount'))}}
Or directly try
#php
$sum = 0
#endphp
#foreach ($team->competitionPayments as $amount)
$sum = $sum + $amount->amount
#endforeach
I want to show data in tabular form in Laravel Blade View. Now, data is not showing up in blade, but when I check in controller using print_r it shows up Following is the code for Controller:-
public function index(){
$pickup = PickupLocation::select('*')
->whereNull('deleted_at')
->get()
->toArray();
$page = 'pickup_locations';
return view('backEnd.pickupManagement.index',compact('page'));
}
Following is the code in web.php:-
Route::match(['get','post'],'pickup-locations','backEnd\pickupManagement\PickupController#index');
And following is the code I am using View:-
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th width="13%">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($pickup))
#foreach($pickup as $key=>$value)
<tr>
<td>{{ ucfirst($value['name']) }}</td>
<td>{{ ucfirst($value['contact_no']) }}</td>
<td>{{ $value['location'] }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
add $pickup variable as second argument in your compact method.
Try like this
return view('backEnd.pickupManagement.index',compact('page', 'pickup')); }
Happy codding
new to Laravel, and just having this problem that I couldn't figure it out.
I have a post controller set up to query out all blog posts. On the database, I have a integer column and named as "status", 0 as inactive, 1 as active. The question is how and where to place the logic to check whetherthe status is 0 or 1, and show "inactive" or "active" in the view. Thanks in advance.
public function index()
{
$posts = Post::where('user_id', 1)->orderBy('id', 'desc')->paginate(5);
return view('post.index')->withPosts($posts);
}
Here is the view
<table class="table">
<tr>
<th>Date Created</th>
<th>Title</th>
<th>Status</th>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td></td>
</tr>
<br>
#endforeach
</table>
you can have that logic inside your blade template:
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td>#if($post->status) active #else inactive #endif</td>
</tr>
<br>
#endforeach
I am using laravel pagination. When I click on next page or any page number, nothing happens.
Controller function
public function getIndex()
{
$articles = Inspector::paginate(15);
return view('admin.inspector.test', compact('articles'));
}
View file
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}
Any help is much appreciated...Thanks...
public function getIndex()
{
return view('admin.inspector.test');
}
just return view while calling the action. dont send any data from the controller, just load the view,
when the view loads, fetch the db connection with pagination method. and render the pagination as below
<?php $articles = DB::connection('table_name')->where('if any condition')->get(); ?>
<table class="table table-bordered" id="mytable">
<thead>
<tr>
<th>Sr. No. </th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{ $article->id }}</td>
<td>{{ $article->email }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $articles->render() !!}