foreach loop executing one by one in laravel - php

I have a view in which I have to execute foreach loops. The problem is that I want to get one input from first foreach then get the other input from the second foreach. I have an outer for loop which will execute until all the foreach is exhausted.
#for($i=0;$i<=$n;$i++)
<tr>
<th scope="row">{{$i}}</th>
#foreach($Names as $Name)&&
<td>{{$Name}}</td>
#endforeach
#foreach($users as $user)
<td>{{$user}}</td>
#endforeach
#endfor

try like this
#foreach($Names as $key => $val)&&
<td> {{$val}}</td>
<td> {{$users[$key]}} </td>
#endforeach

Your making a mistake with your structure here. You should have the Users and there names and username in one variable so you can foreach this result.
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->user }}</td>
</tr>
#endforeach
If you want to add numbers to the list just add a counter with css or php if thats your way.
<?php $i=1; ?>
#foreach($users as $user)
<tr>
<td>{{ $i }}
<td>{{ $user->name }}</td>
<td>{{ $user->user }}</td>
</tr>
<?php $i++; ?>
#endforeach

thanks for trying guys. I got the answer...
#foreach($Names as $Name)
<tr>
<th scope="row">{{$i++}}</th>
{{--#foreach(#Names as $Name)--}}
<th> {{$Name}}</th>
#foreach($users as $user )
<td> {!! Form::text($users.$i,null, array('class' => 'form-control','id'=>$i)) !!} </td>
#endforeach
</tr>
#endforeach
The catch was that for loop and foreach required the same number of iterations. So we can simply eliminate for loop.

Related

How can i add two numbers in foreach blade in laravel and display the total

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

Laravel view foreach with two properties

i need help with my foreach loop in view
Controller.php
$expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id');
$categories = Category::all();
return view('dashboard.index', compact('expenses','categories'));
index.php
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
<td>{{ $expense }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I get the output like this.
enter image description here
It outputs the expense 2 times, is there a limiter for "foreach loop" or is there another way on doing this?
You need to add a condition for that $category->id is the same as category_id
#foreach($expenses as $key => $expense)
#if($key == $category->id)
<td>{{ $expense }}</td>
#endif
#endforeach
The right way to do it is by using join or relationship.
$expenses = Expense::join('categories','categories.category_id','expenses.category_id')->groupBy('expenses.category_id')->selectRaw('sum(expenses.amount) as sum, expenses.category_id','categories.name')->get();
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($expenses as $expense)
<th>{{ $expense->name }}</th>
<td>{{ $expense->sum }}</td>
#endforeach
</tbody>
</table>
You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty
#foreach ($element['subs'] as $item)
#if($loop->index < 10)
// Your code
#endif
#endforeach
You should compare category_id with category->id.
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
#if($expense->category_id == $category->id )
<td>{{ $expense->sum }}</td>
#endif
#endforeach
</tr>
#endforeach

Foreach loop (laravel blade)

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

Check if no records exist in view laravel

Okay so I've hit a wall for some reason and I can't figure this out. I wan't to check if there are any records in a query and then use an #if in my view to say either "yes, there are records" or "none". Nothing I've tried is working..
What I want:
<div class="col-md-12 col-style">
<h1 class="no-border">YOUR LATEST CONVERSIONS</h1>
#if(something here that lets me test if $transactions is empty)
<table class="table table-striped">
<tr>
<th>#</th>
<th>Type</th>
<th>User</th>
<th>Amount</th>
<th>Value</th>
<th>Result</th>
<th>Result Value</th>
<th>Date</th>
</tr>
#foreach($transactions as $transaction)
<tr>
<td>{{ $transaction->id }}</td>
<td>{{ $transaction->status }}</td>
<td>{{ $transaction->username }}</td>
<td>{{ $transaction->amount }}</td>
<td>{{ $transaction->value }}</td>
<td>{{ number_format($transaction->result, 2) }}</td>
<td>{{ $transaction->result_value }}</td>
<td>{{ $transaction->created_at }}</td>
</tr>
#endforeach
</table>
#else
yo no records
#endif
</div>
My controller:
public function index($username)
{
$user = User::where('username', $username)->firstOrFail();
$id = $user->id;
$transactions = Transactions::where('user_id', '=', $id)->take(5)->get();
return view('user.profile', compact('user', '=', 'userCurrency', 'transactions'));
So if you see in the top part there's a basic #if. Maybe I'm over thinking it, or am I passing in something that cant be checked the way I want it to?
Thanks!
#if (!$transactions->isEmpty())
...
#endif
this use laravel collection method do the trick.
it is as easy as:
#if(count($transactions) > 0)
#if($transactions) should do the trick.

Laravel Eloquent where function doesn't work(?)

If got the following database table:
Now I want to display the rows where where "inventory_warning" is greater then the "inventory" field. Like the second results as show below:
In my first example I do a where statement using Eloquent and try to get the rows, but it doesn't yield correct results (it retrieves all rows). My second example shows the check done with a PHP-if statement, which works but will be very inefficient when the table contains more data..
Shortened code:
<?php $products = Product::where('inventory_warning', '>', 'inventory')->get();?>
<tr>
<th colspan="3">First results:</th>
</tr>
#foreach($products as $product)
<tr>
<td># {{ $product->id }} - {{$product->item->name}}</td>
<td>{{ $product->inventory }}</td>
<td>{{ $product->inventory_warning }}</td>
</tr>
#endforeach
<?php $products = Product::all();?>
<tr>
<th colspan="3">Second results:</th>
</tr>
#foreach($products as $product)
#if($product->inventory_warning > $product->inventory)
<tr>
<td># {{ $product->id }} - {{$product->item->name}}</td>
<td>{{ $product->inventory }}</td>
<td>{{ $product->inventory_warning }}</td>
</tr>
#endif
#endforeach
What is going on here? Is this a possible Eloquent bug or am I doing something wrong?
Comparing two database fields isn't supported by the where function as far as i know.
You'll have to use whereRaw
Product::whereRaw('inventory_warning > inventory')->get();

Categories