Hi I'm new to laravel and can't understand sections
very well, but I am trying to display a specific one in a loop only once and then repeat everything else, if a certain condition is true. Here is the code I'm trying to modify. Here i commented the line which i want only one time.
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?>
@if ($related->count() > 0)
@section('tab-titles')
//i want to display this LI once if true
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach
You should use an else since you only want to display it once
Check documentation here link
@if ($related->count() > 0)
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@else
@section('tab-titles')
//i want to display this LI once if true
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
@endif
You can add one simple variable to put a check if a section is been displayed before or not.
$check = 0;
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?>
@if ($related->count() > 0)
if($check==0)
{
@section('tab-titles')
//i want to display this LI once if true
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
$check++;
}
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach
Don't know it is the most elegant solution or not. But you can achieve what you want as follows:
<?php $repeat = true; // first initialize as true ?>
@foreach ($pageTypes['news-events']->categories as $cat)
<?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id);
?>
@if ($related->count() > 0)
@if($your_certain_condition == true && $repeat)
<?php $repeat = false; ?>
@section('tab-titles')
<li class="tab first"><a href="#news" title="insight">Insight</a></li>
@append
@endif
@section('news-content')
<div id="news" class="tabs_text">
<h2>{{ strtoupper($cat->name) }}
</h2>
<ul>
@foreach($related as $news)
<li><a href="{{ url($news->url) }}" title="{{ $news->title }}">
{{ $news->title }}
</a>
</li>
@endforeach
</ul>
</div>
@append
@endif
@endforeach