I'm working with Laravel 5.8 and I have this syntax:
#php($counter_foreach = 0)
#foreach($memnames as $mem=>$memname)
#php
#endphp
#endforeach
Now when I run this, I get syntax error, unexpected 'endforeach' (T_ENDFOREACH) error:
But when I remove #php #endphp, and replace it with html, the error will be gone!
So what's going wrong here?
Is there anything wrong about using #php after #foreach in Laravel Blades?
Try to add #endphp everytime you add #php like this:
#php($counter_foreach = 0)
#endphp
#foreach($memnames as $mem=>$memname)
#php
#endphp
#endforeach
You actually do not need to make separate php variable for the iterations, so since you have a foreach loop already your logic should be something like this.
#foreach($this as $that) {
#if ($loop->first)
// some logic
#endif
#if ($loop->last)
// some logic
#endif
#endforeach
The $loop variable is available inside the foreach and you check for the iteration number you need inside, I just added some basic uses. Here is some more useful documentation.
Related
I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
How to print limited data with FOREACH
I have 10 data in db but, i want to print only first 3 data with foreach.
Also i tried and used array_slice() method, but next i got some errors.
Thank you!
#foreach($products as $_product)
//there is Html code... with variables
#foreach
I tried : #foreach(array_slice($products, 0, 2) as $_product). and i got:
array_slice() expects parameter 1 to be array, object given.
You can use limit(3) in your eloquent or take(3)
Or if you need to make it in blade use $loop variable
Like this
#if($loop->iteration <=3)
#continue
Or in your controller
Product::limit(3)->get();
Product::take(3)->get();
If u use it in controller there will be no need to check in your blade view
Just use the #break; statement once you've printed however many you want - it will jump you out of the foreach loop.
so something quick i can think of this:
#foreach($products as $_product)
//there is Html code... with variables
#if($loop->iteration == 3) //Thanks to the response of #MohammedAktaa
#break
#endif
#foreach
Although I think it should be best to limit the results from the query to the database.
Ordering, Grouping, Limit, & Offset
I'm working with https://github.com/cntaoyu/CI-Blade framework and my code is : (works fine and prints too)
#foreach($tbl_data as $d)
#foreach($d as $c)
{{$c}} //I want space after this, new line in fact
#endforeach
#endforeach
#foreach($tbl_data as $d)
#foreach($d as $c)
{{$c}} <br> //if it is blade
#endforeach
#endforeach
This is data I have:
but with this blade template code:
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{$order->id}}</td> //51 line
<td>{{$order->order->b_first_name}} {{$order->order->b_last_name}}</td>
<td>{{$order->order->r_first_name}} {{$order->order->r_last_name}}</td>
<td>£{{$order->total}}</td>
<td>{{$order->validity->diffForHumans()}}</td>
</tr>
#endforeach
</tbody>
I got this error:
ErrorException in fbe60fc17d23b35313269a941fc4d6f0 line 51: Trying to
get property of non-object (View:
/home/dgadmin/public_html/test/resources/views/admin/expired.blade.php)
What is the problem here? Why I cant use #foreach loop ?
First, go into your /storage/framework/views folder and look at line 51 for the file called fbe60fc17d23b35313269a941fc4d6f0. I see you said that it was $order->id, but I was not sure if you checked the compiled view that is actually served or admin/expired.blade.php. The compiled view is the one throwing the error, so the line number will correspond with the compiled view, not the blade you are making. When you look on this line you will see what is missing. This error is thrown because you are trying to access a null value on the model. If you want to go the lazy route, change your loop to this:
<tbody>
#foreach ($orders as $order)
<tr>
<td>{{isset($order->id)? $order->id: ''}}</td> //51 line
<td>{{isset($order->order->b_first_name)? $order->order->b_first_name : '' }} {{isset($order->order->b_last_name)? $order->order->b_last_name : ''}}</td>
<td>{{isset($order->order->r_first_name)? $order->order->r_first_name : ''}} {{isset($order->order->r_last_name)? $order->order->r_last_name : ''}}</td>
<td>£{{isset($order->total)?$order->total : ''}}</td>
<td>{{$order->validity->diffForHumans()}}</td>
</tr>
#endforeach
</tbody>
This will print the loop as expected, changing any missing values (like the one that triggered this errror) to a blank string.
Simple view not rendering:
public function getFranchise() {
echo 'getFranchise';
$franchiseData['shopViews'] = array(1 => 'a');
$franchiseData['aaa'] = 'bbb';
return View::make('test.filteredData.franchise', $franchiseData);
}
view in test/filteredData/franchise.blade.php
franchise
{{--$shopsViews}} {{-- Fatal error: Method Illuminate\View\View::__toString() must not throw an exception in D:\projektai\dashboard\app\storage\views\d2973247ea68aed2fdfd33dc19cccada on line 5}}
{{ $aaa }}
#foreach ($shopsViews as $shop)
<strong>aaa</strong>
#endforeach
Only word getFranchise is displayed which means controller function is called. No any errors, no anything. WHat is that?
Even in constructor is added
ini_set("display_errors", true);
Edited
Found that this:
{{--$shopsViews}} {{-- Fatal error: Method Illuminate\View\View::__toString() must not throw an exception in D:\projektai\dashboard\app\storage\views\d2973247ea68aed2fdfd33dc19cccada on line 5}}
comment was causing stop of execution in the script. WHy is that? this is valid laravel comment. Also I noticed weird thigs when I comment
<?php //print_r ?>
then it shows something like web page not found, like interent connection has gone. I dont get at all whats happening with commenting.
Your blade view must contain #extends() and #section() to work in this case.
And comment should look like this {{-- $shopsViews --}}. That should fix your problem.
#extends('your_layout_folder.layout')
#section('content')
#foreach ($shopsViews as $shop)
<strong>aaa</strong>
#endforeach
#stop
Please follow the documentation! http://laravel.com/docs