Blade Array

Blade Array

Array syntax is Laravel is same as in another, here we pass array as a second argument in view and create that array outside Like below and call this array either using any loop but we use foreach loop that do the work simple for an array.

There are couple of things that we can access in foreach loop like we can call the property index using $loop variable with the help of -> not [] because this is not an object.

{{$loop->index}}

In this way we can use another property like first and last similar to index and create any logic like if and anything else.

@if($loop->first)

<p>First in the loop</p>

@endif

Route Example

Route::get(‘/contact’, function () {

    $courses = [

        [‘name’ => ‘php’, ‘fees’ => 2500],

        [‘name’ => ‘html’, ‘fees’ => 1000],

        [‘name’ => ‘JavaScript’, ‘fees’ => 1500],

    ];

    return view (‘contact’, [‘courses’ => $courses]);

});

View Example

    @foreach($courses as $course)

                <p> {{$loop->index}}   {{$course[‘name’]}}——–{{$course[‘fees’]}}</p>

                        @if($loop->first)

                        <p>First in the loop</p>

                        @endif

                        @if($loop->last)

                        <p>Last in the loop</p>

                        @endif

    @endforeach

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*