ErrorException为foreach()Laravel提供的参数无效

问题描述 投票:1回答:5

我只是想从我的表名帖子中获取所有数据但是发生错误它不会让我在index.blade.php上显示数据

public function index()
    {
        //
        $posts = Post::all();

        return view('posts.index')->withPosts('$posts');

    }

这是我的index.blade.php

@foreach( $posts as $post )
            <div class="post-preview">
                    <a href="post.html">
                        <h2 class="post-title">

                            {{ $post->title }} -> this is the one i want to display

                        </h2>
                        <h3 class="post-subtitle">

                            {{ $post->body }}

                        </h3>
                    </a>
                    <p class="post-meta">Posted by <a href="#">Start Bootstrap</a> on September 24, 2014</p>
                    <button type="button" class="btn btn-secondary">&nbsp;&nbsp;Edit&nbsp;&nbsp;&nbsp;</button>
                    <button type="button" class="btn btn-secondary">Delete</button>
            </div>

                <hr>
            @endforeach
laravel foreach blade
5个回答
1
投票

你应该用它作为

return view('posts.index')->with(compact('posts'));

1
投票

你最好像这样使用with

return view('posts.index')->with('posts',$posts);

0
投票

不要在$ posts中使用单引号

return view('posts.index')->withPosts($posts);

或尝试第二种方式

return view('posts.index', compact('posts'));

0
投票

你可以使用compact方法

return view('posts.index',compact('posts'));

0
投票

如果其他解决方案无效,请尝试此操作。

我可能认为你在表单中错过了这一行“enctype =”multipart / form-data“。 见下面的例子:

<form action="{{ route('upload.files') }}" method="POST" enctype="multipart/form-data">
<input id="fileupload" type="file" name="attachment[]" multiple>
</form>

别忘了单击向上箭头。我希望我能帮助你:)

© www.soinside.com 2019 - 2024. All rights reserved.