Invalid argument supplied for foreach() in Laravel - laravel

I have double foreach here. Here is my view
<table border="1">
<tr>
<th>ID</th>
<th>ISBN</th>
<th>Klasifikasi</th>
<th>Lokasi</th>
<th>Cp_Or</th>
<th>Tahun</th>
<th>ID_Master_Buku</th
<th>Jenis</th>
<th>Status</th>
<th>Tgl_Masuk</th>
<th>can_issue</th>
<th>ID</th>
<th>Edisi</th>
<th>Pengarang</th>
<th>Deskripsi</th>
<th>Penerbit</th>
<th>Judul</th>
<th>Jumlah_Buku</th>
<th>Bahasa</th>
<th>Gambar</th>
<th>Subjek</th>
<th>Topik</th>
</tr>
#foreach($bukus as $buku)
#foreach($buku->tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>
<td>{{$buku->Lokasi}}</td>
<td>{{$buku->Cp_Or}}</td>
<td>{{$buku->Tahun}}</td>
<td>{{$buku->ID_Master_Buku}}</td>
<td>{{$buku->Jenis}}</td>
<td>{{$buku->Status}}</td>
<td>{{$buku->Tgl_Masuk}}</td>
<td>{{$buku->can_issue}}</td>
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>
<td>{{$item->Penerbit}}</td>
<td>{{$item->Judul}}</td>
<td>{{$item->Jumlah_Buku}}</td>
<td>{{$item->Bahasa}}</td>
<td>{{$item->Gambar}}</td>
<td>{{$item->Subjek}}</td>
<td>{{$item->Topik}}</td>
#endforeach
</tr>
#endforeach </table>
It works in my other views. But here, I got an error
Invalid argument supplied for foreach()
Could you please help me? Thanks in advance

Sounds a lot like at least one $buku->tmbuku is null...
Try wrapping it in an #if:
#foreach($bukus as $buku)
#if($tmbuku = $buku->tmbuku
#foreach($tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>
<td>{{$buku->Lokasi}}</td>
<td>{{$buku->Cp_Or}}</td>
<td>{{$buku->Tahun}}</td>
<td>{{$buku->ID_Master_Buku}}</td>
<td>{{$buku->Jenis}}</td>
<td>{{$buku->Status}}</td>
<td>{{$buku->Tgl_Masuk}}</td>
<td>{{$buku->can_issue}}</td>
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>
<td>{{$item->Penerbit}}</td>
<td>{{$item->Judul}}</td>
<td>{{$item->Jumlah_Buku}}</td>
<td>{{$item->Bahasa}}</td>
<td>{{$item->Gambar}}</td>
<td>{{$item->Subjek}}</td>
<td>{{$item->Topik}}</td>
</tr>
#endforeach
#endif
#endforeach

Try checking if its null and items count before executing the second loop e.g.:
#foreach($bukus as $buku)
#if(!is_null( $buku->tmbuku ) && count( $buku->tmbuku ) )
#foreach($buku->tmbuku as $item)
<tr>
<td>{{$buku->ID}}</td>
<td>{{$buku->ISBN}}</td>
<td>{{$buku->Klasifikasi}}</td>....
<td>{{$item->ID}}</td>
<td>{{$item->Edisi}}</td>
<td>{{$item->Pengarang}}</td>
<td>{{$item->Deskripsi}}</td>...
</tr>
#endforeach
#endif
#endforeach

Related

Can we Put 3 database data in One Table

I am self learner and stuck at one point. I don't know is that possible or not. Please refer image of data get in array.
I have 3 database table "animalsell", "dairyincomes" and "othersell". I sucessfully able to get required data from this.
Now I want to put the same in one table in blade file (refer table image)
Controller file where i collect the data
$fiveincomedata = Dairyincome::select(
DB::raw('DATE_FORMAT(milksaledate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(milksaledate) as milksaledate')
)
->whereBetween('milksaledate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('milksaledate', 'desc')
->get()->toArray();
// Income from Animal Sell
$fiveanimalselldata = Animalsell::select(
DB::raw('DATE_FORMAT(selldate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(selldate) as selldate')
)
->whereBetween('selldate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('selldate', 'desc')
->get()->toArray();
// Income from Other Sell
$fiveotherselldata = Othersell::select(
DB::raw('DATE_FORMAT(saledate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(saledate) as saledate')
)
->whereBetween('saledate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('saledate', 'desc')
->get()->toArray();
Blade File
<div class="card-body">
<table id="incometable" class="table m-0 table table-bordered table-hover table" style="width:100%">
<thead>
<tr>
<th>Date</th>
<th>Amount (Milk Sale)</th>
<th>Amount (Animal Sale)</th>
<th>Amount (Other Sale)</th>
<th>Total Amount </th>
</tr>
</thead>
<tbody>
#foreach ($fiveincomedata as $fivei )
<tr>
<td>{{ $fivei->month_name }}</td>
<td>{{$fivei->totalamount}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Hope i explain my problem clearly and thanks for help in advance.
$a1=array("red","green");
$a2=array("blue","yellow");
array_merge($a1,$a2);
or
push the data into a common array.
You can use the merge() function.
For example:
$fiveincomedata = Dairyincome::all();
$fiveanimalselldata = Animalsell::all();
$fiveotherselldata = Othersell::all();
$merged = $fiveincomedata->merge($fiveanimalselldata);
$merged = $merged->merge($fiveotherselldata);
$array = $merged->toArray();
More information: https://laravel.com/docs/9.x/collections#method-merge

ColdFusion dataTable returning f is undefined

I'm adding dataTable to my coldFusion project, but it is returning: Uncaught TypeError: f is undefined
Code:
<table id="webPosttable" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>DATE</th>
<th>CK</th>
<th>NAME</th>
<th>IN</th>
<th>RATE</th>
<th>COST</th>
</tr>
</thead>
<tbody>
<cfoutput query="myQuery">
<cfset totalreportin = totalreportin + val(counter)>
<cfset totalreportcost = rate*counter + totalreportcost>
<tr>
<TD>#inserteddate#</TD>
<TD>#ck#</TD>
<TD>#fullname#</td>
<TD>#counter#</TD>
<td>#decimalformat(rate)#</td>
<td>#dollarformat(rate*counter)#</td>
</tr>
</cfoutput>
</tbody>
<tfoot>
<cfoutput>
<tr>
<TD colspan="3">TOTAL:</TD><td>#totalreportin#</td><TD></td><td>#dollarformat(totalreportcost)#</td>
<TD colspan="3">AVERAGE:</TD><td><Cfif incomingreport.recordcount GT 0>#decimalformat(val(totalreportin/incomingreport.recordcount))#<Cfelse>0</CFIF></td>
</tr>
<tr>
<td></td><td><Cfif totalreportin GT 0>#dollarformat(totalreportcost/totalreportin)#<cfelse>0</cfif></td>
</tr>
</cfoutput>
</tfoot>
</table>
<script>
$('#webPosttable').DataTable({
"lengthChange": false,
"paging": false,
"bInfo" : false,
"dom": '<"pull-left"f><"pull-right"l>tip'
});
</script>
Does anyone know what if something is missing in my table structure or javascript datable settings?
Thanks
The problem won't be in your Coldfusion code, it will be the structure of your <tfoot> content. The number of columns in the tfoot doesn't match the number of columns in the rest of your table. Even the two trs within your tfoot don't match each other.
Comment out the tfoot temporarily to test whether it works without, then balance up the columns and put it back in.
eg:
<tfoot>
<cfoutput>
<tr>
<TD>TOTAL:</TD>
<td>#totalreportin#</td>
<td></td>
<td>#dollarformat(totalreportcost)#</td>
<TD>AVERAGE:</TD>
<td><Cfif incomingreport.recordcount GT 0>#decimalformat(val(totalreportin/incomingreport.recordcount))#<Cfelse>0</CFIF></td>
</tr>
<tr>
<td colspan="5"></td>
<td><Cfif totalreportin GT 0>#dollarformat(totalreportcost/totalreportin)#<cfelse>0</cfif></td>
</tr>
</cfoutput>
</tfoot>
If you still have errors after that then I'd advise updating the question to include the code showing which version of jQuery+datatables you're including and where and how you're including it. You may also need to wrap your script in a $(document).ready( function () { ...

Laravel Building Referral System- Cant display Users in Blade

I Building an Affiliate System for my Users. Registration with ID works fine and save all correct in my DB, but i cant see any Referrals that use my URL ID for Registration in my Example Blade.
Any one cant find the Error?
Controller:
$referal_id = $user->id;
$referer_id = $request['referal_id'];
$referalUserData = [
'referer_id' => $referer_id,
'referal_id' => $referal_id,
];
$insert = ReferralUser::insert($referalUserData);
Blade:
<tr>
<th>#</th>
<th>User Id#</th>
<th>User Name</th>
<th>Registration Time</th>
</tr>
</thead>
<tbody>
#if(!empty($referalUser))
#foreach($referalUser as $referData)
<tr>
<th scope="row">{{$referData->id}}</th>
<td>{{$referData->referal_id}}</td>
<td>#for($i=0;$i<count($referData['names']);$i++) {{$referData['names'][$i]['name']}} #endfor
</td>
<td> {{$referData->created_at}}<br/> <span class="label label-primary">
{{Carbon\Carbon::createFromTimestamp(strtotime($referData->created_at))->diffForHumans()}} </span> </td>
</tr>
#endforeach
#else
<div class="alert alert-noreferals">
<strong></strong><span>No referrals!</span>
</div><br/><br/>
#endif
Ignore the created_at Timestamp.
Thanks
Did you convert the $request object to an array?
$request['referal_id'];
That looks suspicious. Typically you would use
$request->input('referal_id')
or via dynamic properties
$request->referal_id
To access a given value.
Are you getting an error? Anything in the log files?

Laravel Blade change row color when variable value is different in loop

I have a report page that loops through a collection and displays the data in a generic bootstrap template table - it's showing data about users, and one user can have multiple rows in the table. I group the rows by the user ID and I want to basically stripe the table rows based on the user ID (not just alternating rows). So, for example, I might display 4 rows for user ID = 5 with a white background and then 2 rows for user ID = 6 with a gray background. I'd just use a local variable in straight php, but is there an elegant/clean way to do this in the blade templates?
#foreach($payments->getDetails() AS $k=>$detail)
<tr>
<td>{{ $detail->payment->userid }}</td>
<td>${{ $detail->payment->amount }}</td>
<td>{{ $detail->payment->status }}</td>
</tr>
#endforeach
In this example, $payments is a collection of $paymentsDetails objects. So based on the $detail->payment->userid value I'd like to determine the class or background color.
Thank you for any help in this.
Just a thought... This assumes the rows are ordered by user id naturally (it would not work otherwise in your example anyway...)... So here's the thought...
If the user id is odd, then give it a color, if it is even, give it the alternate color... that would work... Sort of ...
#foreach($payments->getDetails() AS $k=>$detail)
<tr>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->userid }}</td>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->amount }}</td>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->status }}</td>
</tr>
#endforeach
Something like this would also work... not sure I like it though
#foreach($payments->getDetails() AS $k=>$detail)
#if($detail->payment->userid % 2 == 0)
<tr>
<td class="evencolor">{{ $detail->payment->userid }}</td>
<td class="evencolor">>${{ $detail->payment->amount }}</td>
<td class="evencolor">{{ $detail->payment->status }}</td>
</tr>
#else
<tr>
<td class="oddcolor">{{ $detail->payment->userid }}</td>
<td class="oddcolor">${{ $detail->payment->amount }}</td>
<td class="oddcolor">{{ $detail->payment->status }}</td>
</tr>
#endif
#endforeach
Anyway... just a possible direction... someone else might have a more elegant idea...
I got the solution to this via www.laracasts.com from user #Cronix:
I'd do it the way you were thinking originally and just use
#php/#endphp blade directives to determine if the userid has changed
during each loop iteration and if it does change the color.
#php
$lastid = null;
$rowclass = 'grey';
#endphp
#foreach($payments->getDetails() AS $k=>$detail)
#php
//if userid changed from last iteration, store new userid and change color
if ($lastid !== $detail->payment->userid)
{
$lastid = $detail->payment->userid;
if ($rowclass == 'grey') $rowclass = 'white';
else $rowclass = 'grey';
}
#endphp
<tr class="{{ $rowclass }}">
<td>{{ $detail->payment->userid }}</td>
<td>${{ $detail->payment->amount }}</td>
<td>{{ $detail->payment->status }}</td>
</tr>
#endforeach
If anyone out there has a more elegant Blade-focused idea on how to solve it, I'd love to hear about it.
hey guys the methods above failed for me, incase anyone els needs this : im using laravel 5.4 with php 5.6===> this is what has worked for me;
#php
if ( $post->dbdata == 'approved'):
$color = 'green';
elseif ( $post->dbdata == 'Pending'):
$color = 'yellow';
elseif ( $post->dbdata == 'rejected'):
$color = 'red';
else:
$color = 'black';
endif;
#endphp
<tr style="background-color: {{$color}}">
<td>
<h6>{{ $post->dbdata }}</h6>
</td>
</tr>

Laravel: How can I set dynamic table headers from a nested foreach loop?

I have this markup in my blade template:
<th>Name</th>
#foreach($camp->athletes as $athlete)
#foreach($athlete->kickoffs as $key=>$kickoff)
<th>D#{{ ($key+1) }}</th>
<th>H#{{ ($key+1) }}</th>
#endforeach
#endforeach
<th>Avg. Distance</th>
<th>Avg. Hang Time</th>
<th>Score</th>
Which works fantastic when I only have a single athlete. But, when I start adding more, it's obvious that I am going to repeat the <th> elements for each athlete. Something like this is what I am getting:
<th>D#1</th>
<th>H#1</th>
<th>D#2</th>
<th>H#2</th>
<th>D#1</th>
<th>H#1</th>
<th>D#2</th>
<th>H#2</th>
I totally understand why I am getting duplicates like that - I'm not sure how to avoid that though. It's like I need to do this:
...
<th>Name</th>
#foreach($athlete->kickoffs as $key=>$kickoff)
<th>D#{{ ($key+1) }}</th>
<th>H#{{ ($key+1) }}</th>
#endforeach
<th>Avg. Distance</th>
...
That way I only get this output:
<th>D#1</th>
<th>H#1</th>
<th>D#2</th>
<th>H#2</th>
But I can't access the $athlete variable directly like that.
Here is what I came up with so that my table header would only render once no matter how many athletes I have.
my-view.blade.php
<?php $i = 0; ?>
#foreach($camp->athletes as $athlete)
#foreach($athlete->kickoffs as $key=>$kickoff)
#if(($key+1) > $i)
<th>D#{{ ($key+1) }}</th>
<th>H#{{ ($key+1) }}</th>
<?php $i++; ?>
#endif
#endforeach
#endforeach

Resources