How access to $loop variable in nested foreach blade - laravel

#foreach($last_articles->chunk(3) as $row)
#foreach($row as $last_article)
#if($loop->index!=0) {{"in $loop"}} #endif
#endforeach
#if($loop->index!=0) {{"out $loop"}} #endif
#endforeach
how to access the $loop of any of foreach, that was used in the code?

You could use $loop->parent to access the parent loop of the current loop.

In my case (Laravel 6.8) $loop->parent looks like this
[
{"iteration":1,"index":0,"remaining":1,"count":2,"first":true,"last":false,"odd":true,"even":false,"depth":1,"parent":null},
{"iteration":2,"index":1,"remaining":0,"count":2,"first":false,"last":true,"odd":false,"even":true,"depth":1,"parent":null}
]
to access the parent index $loop->parent->index

Related

How to iterate nested data in laravel blade view?

I have below the draft implementation. The spec is to show the parent information and children-grandchildren information.
#foreach ($parent as $children)
<x-icons.chevron-right>
<h1>{{ $parent->name }}</h1>
#if($person->has('children'))
// go back at the top for nested for each
#endif
#endforeach
One solution that you may recommend is to create an iterative function. But my problem for that, it does not completely render my component-icon chevron-right.
#php
function showHTML($person) {
$html = '';
foreach($person as $children) {
$html .= `
<x-icons.chevron-right>
<h1>$person->name</h1>
`;
if ($person->has('children')) {
$html .= showHTML($person->children);
}
}
return $html;
}
#endphp
{!! showHTML($person) !!}
Just wondering if you guys have other solution for this to show nested with a component icon? I would appreciate any answer.
I encountered the same thing, and it did not take me long to figure out that we can re-iterate a component.
In my case, I just created a root component and re-iterated it if it had children.
So I don't need to loop it any longer.
So my usage would like this below:
<x-parent person="$person"></x-parent>
And my root component parent.blade.php. So If there is person has children I just re-iterate the component and pass his children as a prop:
#props(['person'])
<x-icons.chevron-right>
<h1>{{ $person->name }}</h1>
#if($person->has('children'))
<x-parent person="$person->children"></x-parent>
#endif
You can try => value on your foreach, that will fetch data array value from backend.
Or can you show your code on backend (controller), that can easy to help you
#foreach ($parent as $children => $value)
<x-icons.chevron-right>
<h1>{{ $parent->name }}</h1>
<h3>{{ $value->data }}<h3>
#endforeach

Trying to get property of non-object error, count always return 1 even no empty array

I am displaying my data using datatable, if empty array then datatable will not be shown.
I tried
#if(count($results) > 0)
//data show
#foreach($results as $r)
<p>{{$r->name}}</p>
#endfor
#else
No result found.
#endif
//
// $results is an array
This will return error if empty $result. How can i make it if $result is empty then show the custom message and not to display DataTable.
You should try this:
#if(isset($results) && !empty($results))
<?php
print('<pre style="color:red;">');
print_r($results);
print('</pre>');
?>
#foreach($results as $r)
<p>{{$r->name}}</p>
#endforeach
#else
No result found.
#endif
We do have a shorthand in blade for the foreach/empty scenarios.
#forelse ($results as $r)
<p>{{ $r->name }}</p>
#empty
No result found
#endforelse
From the code you have and the comment you have of how you get $results, $results should be an array of stdClass objects, which means that error should not be happening. Since the code you have is limited, I can't be sure the error is coming from that code at all.
My guess is that $results isn't an array but is an object. count(new stdClass) === 1 count(new App\User) === 1, etc ...
You are ending the foreach loop with endfor. It should be ended with endforeach.
If this do not fix your error then please update your question with your controller code. Is your results variable a collection object?
Maybe is safer to check at the controller level and return 0 if $result is NULL
After you get the collection just check this:
$result = $result ? $result : 0
And return $result.
Use isEmpty() to check object empty or not-
#if (!$result->isEmpty())

How to set null if relations doesn't exist?

In Model I have:
public function publisher()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
In template blade I try to show data User:
#foreach($announcement->offers as $key => $item)
<img src="{{url($item->publisher->photo)}}">
#endforeach
The Problem is that if there is no data in the User table, the program crashes because I can not get the property $item->publisher->photo.
How to fix it?
You can do something like this:
#if (empty($item->publisher))
It's empty
#else
<img src="{{ url($item->publisher->photo) }}">
#endif
Working on memory here, but you could use #forelse/ #empty. These directives can be used if the object your'e iterating over is empty:
#forelse($announcement->offers as $key => $item)
<img src="{{url($item->publisher->photo)}}">
#empty
//default image or message?
#endforelse
https://laravel.com/docs/5.4/blade#loops

Laravel Blade loop through config array variable

In my config/locale.php I have an array 'displayLanguage' which contain key=>value pairs
How can I loop through this array in blade ?
I have tried the following
#foreach ( {{ Config::get('app.locale.displayLanguage') }} as $itemKey => $itemVal)
{{ $itemKey }}
#endforeach
I am getting syntax error, unexpected '<'. tried also some other veriation to loop this var without passing it through the controller
If your file is in config/locale.php then you call config('locale.displayLanguage');
#foreach(config('locale.displayLanguage') as $key => $value)
{{ $key }}
#endforeach
I am using the global helper config() in a blade file.
It also appears you have extra curly braces in your foreach loop

Laravel blade #include view using variable

I have a few blade template files which I want to include in my view dynamically based on the permissions of current user stored in session. Below is the code I've written:
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
#include('dashboard.tiles.' . $tile)
#endif
#endforeach
Blade is not allowing me to concatenate the constant string with the value of variable $tile. But I want to achieve this functionality. Any help on this would be highly appreciated.
You can not concatenate string inside blade template command. So you can do assigning the included file name into a php variable and then pass it to blade template command.
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
<?php $file_name = 'dashboard.tiles.' . $tile; ?>
#include($file_name)
#endif
#endforeach
Laravel 5.4 - the dynamic includes with string concatenation works in blade templates
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
#include('dashboard.tiles.' . $tile)
#endif
#endforeach

Resources