0

I've two fields in database named "Events dates" and "Event descriptions". It stores an array of collection of dates and descriptions.

Now, I want to show them on timeline in a way that each description should be displayed in front of it's date.

For example, First date must have only Event 1. Second date must have only Event 2 and so on. But I' m not getting the desired output. My dates are showing correctly but description doesn't looks like it supposed to be.

See

enter image description here

How can I output single description for each date?

Blade

<div class="expanded-list">
    <div id="faq6" class="collapse" data-parent=".faq-list">

        @php
            $events = explode (",", $row->get_event_dates);
            $details = explode (",", $row->get_event_descriptions);
        @endphp

        <div class="col-md-6">
            <div class="main-card mb-3 card">
                <div class="card-body">
    
                    @foreach ($events as $key => $val)
                        <div class="vertical-timeline-item vertical-timeline-element">
                            <div class="vertical-timeline-element-content bounce-in">

                                @foreach ($details as $key => $detail)
                                    <p>{{ $detail }}</p> 
                                @endforeach
                            
                                <span class="vertical-timeline-element-date">{{ $val }}</span>                            
                            </div>
                        </div>
                    @endforeach

                 </div>
             </div>
        </div>
    </div>
</div>
1
  • Why are these in two separate variables? If you have relationships and queries set up correctly, $events = Event::with("details")->get() and you're done. Don't put logic in your view, prepare variables in your controller and pass them. Commented Oct 10, 2020 at 14:52

1 Answer 1

1

your second foreach loop is iterating for every loop of the first foreach. and printing every value for each item of the first loop. you don't need the second foreach loop. assuming you have same length of both arrays. so just loop once and get the value of the second array using key.

@foreach ($events as $key => $val)
    <div class="vertical-timeline-item vertical-timeline-element">
        <div class="vertical-timeline-element-content bounce-in">
            <p>{{ $details[$key] }}</p> 
            <span class="vertical-timeline-element-date">{{ $va l}}</span>                            
        </div>
    </div>
@endforeach

and you if you somehow don't have the same length for both arrays you can check for array index

@if (isset($details[$key]))
    <p>{{ $details[$key] }}</p>
@else
    <p>--</p>
@endif
Sign up to request clarification or add additional context in comments.

3 Comments

Amazing Job bro !! I was stuck on this issue for last 4 hours and you saved my time. Allah bless you a lot !
Could you please elaborate a little bit like how did you make it work with the $key of different database field ?
I got it. Thanks buddy !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.