user5265806
user5265806

Reputation:

Laravel - Invalid argument supplied for foreach()?

I have this code in my controller:

    public function contact(){

        $people = ['Michael', 'martin', 'Peter', 'Marian'];

        return view('contact', compact('people'));
    }

and in my contact.blade.php:

@extends('layouts.app')

@section('content')

    <h1>Contact Page</h1>

    @if (count($people))

        <ul>

        @foreach(@people as $person)
            
            <li>{{$person}}</li>

        @endforeach

        </ul>

    @endif

@endsection

@section('footer')



@endsection

I am getting the error: Invalid argument supplied for foreach() (View: /home/mao/Documents/blog/resources/views/contact.blade.php)

I do not see the error.. been rewriting it twice. this should be correct as far as i can see on online guides?

Upvotes: 0

Views: 82

Answers (2)

Akbar Rahmanii
Akbar Rahmanii

Reputation: 144

change @people to $people (@ => $)

@extends('layouts.app')

@section('content')

    <h1>Contact Page</h1>

    @if (count($people))

        <ul>

        @foreach($people as $person)
            
            <li>{{$person}}</li>

        @endforeach

        </ul>

    @endif

@endsection

@section('footer')



@endsection

Upvotes: 0

MONSTEEEER
MONSTEEEER

Reputation: 580

I think you just have a mistype in your foreach

Try this

@foreach($people as $person)
    <li>{{$person}}</li>
@endforeach

Change the @ in $

Upvotes: 2

Related Questions