Loop interation inside #each directive - php

I'm trying to iterate over a loop of items which is being included via the #each directive in Laravel Blade.
When I use a regular #foreach loop, this works perfectly fine and I can iterate over odd/even records, but when using #each, this concept doesn't seem to be working.
Am I doing something wrong or is this expected behaviour of the #each directive?
My code is as follows:
splits.blade.php
<section>
#each('_partials/components.split', $page->splits, 'split')
</section>
split.blade.php
<article
#if ($loop->odd)
style="background-image: url('placehold.it/1920x400');"
class="bg-cover bg-center"
#else
class="bg-white"
#endif
>
</article>
Any help would be greatly appreciated.
Many thanks
Matt

Each doesn't include $loop variable in blade file automatically but passes $key variable.
So you can write:
<article
#if ($key % 2 == 0)
style="background-image: url('placehold.it/1920x400');"
class="bg-cover bg-center"
#else
class="bg-white"
#endif
>
</article>

The each directive does not have the $loop variable like foreach.
However, it does send the key of every item in the provided array to the view. So if you did not set custom keys, the $key variable should contain a value from 0 to the length of your array.
Just found this out by looking in the source code.

Related

Prevent foreach from loop html tag multiple time in blade laravel

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 limite foreach loop

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

Empty array don't appear as empty in Laravel blade

Right now I'm trying to check when the view receives an empty array.
#if(! empty($array))
// Section content goes here...
#foreach($array as $value)
// All table data goes here...
#endforeach
#endif
The code as it is above seems to still run when the $array is empty and causes an exception.
When I try to dump the array with {{ dd($array) }} I get $array = [].
Sounds like you might be having a collection. You could simply do count($array) to check amount of records in the array. It would look something like this:
#if(count($array))
// Section content goes here...
#foreach($array as $value)
// All table data goes here...
#endforeach
#endif
The section should now be hidden. If you wish to only skip the foreach when there is nothing in the array you could do this:
// Section content goes here...
#forelse($array as $value)
// All table data goes here...
#empty
// Optional message if it's empty
#endforelse
Which would output the section content and check if the array has any values before it foreach it.
You can read more about loops within blade files in the Laravel documentation.
Is it possible your array is a collection?
Try using a #forelse, this will check if the array or collection is empty and display another block instead. For example:
#forelse($array as $value)
{{ $value }}
#empty
array is empty
#endforelse

Load data in view in Laravel

I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.

php Issue with Intro.js using it in a foreach loop

I have decided to use Intro.js to create a guide for my website.
But I have this issue. If I put Intro.js in a loop it works for all the rows it generate. so each loop generate 50 rows dynamically from result from DB and hence generate 50 intro.js.
Is there way to break; or stop Intro.js not go through the entire loop and focus on one row instead?
#foreach($providers as $key => $provider)
<tr data-intro="{{ TL::helpdesk('viewvilkareachrowdescription') . $provider->name }}"></tr>
#endforeach
Hope its possible? please help anyone?
you can use an if statement to do something on only one row and print the others.
here is an example :
#foreach($providers as $key => $provider)
#if ($provider->name == 'any_name')
... // << case focus on one row
#else
<tr data-intro="{{ TL::helpdesk('viewvilkareachrowdescription') . $provider->name }}"></tr>
#endif
#endforeach
and here is a link to documentation about Blade Templating : https://laravel.com/docs/5.0/templates

Categories