working with Laravel 8 and I have following view file with controller index.blade.php
<div class="row">
    <div class="col-md-12">
        <table class="table">
            <thead>
                <th>#</th>
                <th>Title</th>
                <th>Body</th>
                <th>Created At</th>
                <th></th>
            </thead>
            <tbody>
                @foreach ($posts ?? '' as $post)
                <tr>
                    <th>{{$post->id}}</th>
                    <td>{{$post->title}}</td>
                    <td>{{ substr($post->body,0,50)}}{{ strlen($post->body) > 50 ? "..." : ""}}</td>
                    <td>{{ date('M j, Y', strtotime($post->created_at))}}</td>
                    <td><a href="{{route('posts.show',$post->id)}}" class="btn btn-default btn-sm">View</a><a href="{{route('posts.edit',$post->id)}}" class="btn btn-default btn-sm">Edit</a></td>
                </tr>
              @endforeach
            </tbody>
            
        </table>
    </div>
</div>
and Controller
public function index()
    {
        $posts = Post::all();
        return view('posts.index');
    }
but I got following error message
ErrorException Invalid argument supplied for foreach() (View: F:\2020 technologies\laravel\blog\resources\views\posts\index.blade.php) 
what is wrong with this. how could I fix this matter?