0

I tried this but it got printed in fronted

{{ $x=1}}
@if(count($bookings))
    @foreach ($bookings as $data)
        <tr>
            <td>{!! $x !!}</td>
            <td>{!! $data->service->id !!}</td>
            <td>{!! $data->service->title !!}</td>
            <td>{!! $data->full_name !!}</td>
            <td>{!! $data->email !!}</td>
            <td>{!! $data->phone !!}</td>
            <td>{!! date('d-m-Y', strtotime($data->booking_date))  !!} {!! $data->slot_start_from !!}</td>
        {{$x++}}
    @endforeach

This code not working value of x got printed . i don't want to use code php code here any good suggestion welcome

2

2 Answers 2

3

As @RomanBobrik said, use the loop variable $loop to get the iteration

    @foreach ($bookings as $data)
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>{{ $data->service->id }}</td>
            <td>{!! $data->service->title !!}</td>
            <td>{!! $data->full_name !!}</td>
            <td>{{ $data->email }}</td>
            <td>{!! $data->phone !!}</td>
            <td>{!! date('d-m-Y', strtotime($data->booking_date))  !!} {!! $data->slot_start_from !!}</td>

    @endforeach
Sign up to request clarification or add additional context in comments.

4 Comments

do we need to define $loop variable or it is predefined?
@AkshayKumar it's a loop variable present when you use a blade loop. laravel.com/docs/5.8/blade#the-loop-variable
@AkshayKumar can u help me with this question stackoverflow.com/questions/58990038/…
yeah sure i will try
1

You're looking for loop variable, which exists in Laravel Blade.

https://laravel.com/docs/5.8/blade#the-loop-variable

So, just use:

<td>{!! $loop->index !!}</td>

If you have Laravel 5.2 and lower, there is no loop variables. In that case you can use @php shortcut

@php $x=1 @endphp
...
<td>{!! $x++ !!}</td>

1 Comment

then set please one of answers as correct, to close question

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.