1 year ago

Include a foreach looped variable from controller to blade

Posted 1 year ago by pickab00

This is an interesting question which i have been trying to tie my head around.

I have a foreach loop in my controller which loops through my time stamps to find the time difference from two arrays. A start time and an end time. I amusing carbon.

Now that loop returns the results with no issues. The problem is that i already have a loop inside of blade which takes rows from my db and outputs the data. My question is, how can i send the looped data's result in my controller to the views foreach.

Here is my blades for each:

@foreach($clean as $cleans)

                    <td>{{$diff **Here i want to display the loop from controller but only one result comes because its not being looped**}}</td>
                    @if($cleans->verified == 1)<td class="verified">Yes</td>@else()<td     class="notverified">No</td>@endif
                    @if($cleans->img1 || $cleans->img1 || $cleans->img1 == !null)<td     class="verified">Yes</td>@else()<td class="notverified">No Issues</td>@endif
                    <td><a href="{{route('starlord.show', $cleans->id)}}"><i class="lnr lnr-eye">    </i></a></td>


Here is my Controller (Everything i have written so far):

$cleanCollection = Clean::get(['starttime','endtime']);
    foreach($cleanCollection as $cleanObj){
        $startTime = Carbon::parse($cleanObj->starttime);
        $diff = $cleanObj->endtime->diffInMinutes($startTime);

    $clean = Clean::with('users')->orderBy('verified', 'asc')->latest()->paginate(5);

    return view('admin/view', compact('clean', 'diff'));

In basic i want the $diff variable to loop with the $cleans foreach. Currently $diff is returning only one result and $cleans repeats it inside its loop.

Many Thanks

Please sign in or create an account to participate in this conversation.