【问题标题】:Undefined variable: posts (View: C:\xampp\htdocs\QuickLMSzxc1\QuickLMSzxc1\resources\views\index.blade.php)未定义变量:posts(查看:C:\xampp\htdocs\QuickLMSzxc1\QuickLMSzxc1\resources\views\index.blade.php)
【发布时间】:2019-08-06 03:07:10
【问题描述】:

这是我的代码:

Show.blade.php

PostController.php

Index.blade.php

routes/web.php

我收到一个错误:

【问题讨论】:

  • 您好,您的代码有问题不要上传图片
  • 上传你的代码而不是图片
  • 感谢您的建议,我只是这里的菜鸟。稍后我会上传我的代码。

标签: php laravel laravel-blade


【解决方案1】:

首先检查 $posts 变量是否为空,使用 @if(!empty($posts))

【讨论】:

    【解决方案2】:

    如果您与 posts 一词有关,则表示您无法将其传递到您的视图中

    示例代码:

    public function myMethod(){
    //your $posts variable for querying
    $posts = Post::all();
    
    //you have missed the part of passing the variable posts to the view
    //in this case, we are passing the posts variable to the "myview" view using the compact function
    return view('myview, compact('posts') );
    }
    

    【讨论】:

      【解决方案3】:

      您必须提供有关 route/web.php 的更多详细信息。另外,请上传您的代码而不是图片。我会说你可能错过了一些至关重要的东西。

      请注意,当您访问 127.0.0.1:8000 时,将触发您的 route/web.php 中的 '/' 部分。您可以在 route/web.php 中找到类似 Route::get('/', 'SomeController@index') 的内容。基本上这意味着如果访问“/”(通常是主页),Laravel 将从 SomeController 为您的浏览器索引功能提供服务。因此,只需将您的 route/web.php 更改如下。

      Route::get('/', 'PostController@index');

      您可能会看到 url 为 127.0.0.1:8000 的所需页面

      在Routing-Laravel查看更多信息

      如果不是这种情况,请向我们提供更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        • 2021-02-19
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        • 2021-03-16
        相关资源
        最近更新 更多