Laravel php iteration problem with foreach loop [closed]
Post By : Ramesh Dahal | 1 year ago

2 Posts
0 Replies
Staff

I hope i can ask this one correctly. So i have this list of item i would like to iterate with a foreach loop . For some logic reason i'm not doing this right please help.

This is what my controller is sending to the blade page

$timeBegin = new DateTime('09:00');
$timeEnd   = new DateTime('20:00');
$interval = DateInterval::createFromDateString('60 min');
$datePeriods  = new DatePeriod($timeBegin, $interval, $timeEnd);

return view('home')
    ->with('listOfitems', $listOfitems)
    ->with('datePeriods', $datePeriods);

The $listOfitems contain some hours from 9:00 AM to 20:00PM, BUT some of the hours may not be in there therefore it need to use the $datePeriods which is the one that suppose to fill the blanks.

And this is the blade part i'm not trying to do right.


<ul class="uk-accordion" data-uk-accordion>
    @foreach($listOfitems as $listOfitem)
    @foreach($datePeriods as $datePeriod)
    @if($datePeriod->getTimestamp() == strtotime($listOfitem->date_hour))
    <li>
        <a class="uk-accordion-title">
            <div class="video-play-list">
                <span><img src="{{asset('images/avatars/avatar-4.jpg')}}" alt=""></span>
                <span class="play-btn-trigger">{{$listOfitem->name}}</span>
                <span class="video-post-time">
                    {{\Carbon\Carbon::createFromDate($listOfitem->date_hour)->format('H:i')}}
                </span>
            </div>
        </a>
        <div class="uk-accordion-content">
            <hr class="uk-divider-icon">
            <a href="#">
                <p>User Name</p>
            </a>
            <h6>Show Details</h6>
            <p>{{listOfitem->description}}.</p>
        </div>
    </li>
    @else
    <li>
        <a class="uk-accordion-title">
            <span>
                <div class="video-play-list">
                    @if(!Auth::check())
                    <a uk-tooltip="title: Please Login; pos: left">
                        @else
                        <a href="{{route('TO SOME PLACE')}}">
                            @endif
                            <span><img style="width: 50px" src="{{asset('images/Add.png')}}" alt=""></span>
                            <span class="play-btn-trigger">Sign this</span>
                            <span class="video-post-time"> {{$datePeriod->format('H:i')}}</span>
                        </a>
                </div>
            </span>
        </a>
    </li>
    @endif
    @endforeach
    @endforeach
</ul>

Replies

Per Page
Sort By

1 Posts
2 Replies
Member

I'm looping the same thing insted of one time so i'm getting the "time" on my page twice or more because of the double loop so i need to cut it or something like that. 

Reply By : Raw Mesh | 1 year ago

1 Posts
2 Replies
Member

i think you should only loop over @foreach($datePeriods as $datePeriod) this not $listOfitems

Reply By : Raw Mesh | 1 year ago