未定义的变量:类别(查看:在Laravel中)如何解决此问题

时间:2019-07-30 21:03:32

标签: php laravel-5

嗨! 我使用laravel 5.8在博客的帖子页中遇到了问题。 在首页上,它的工作效果与帖子页面的表单代码相同,但是我创建了错误。为什么 我有首页的路线

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

这是帖子页面

Route::get('/post/{app}' , [BlogController::Class, 'show']);

对于首页和所有其他页面,indexController相同 检索类别,我在所有页面上都使用@include文件,但是当我在帖子页面上使用其他控制器时,会产生问题。

博客控制器

public function show($id)
    {
        $post =Post::find($id)->where('status' , 'publish');
        return view('/post')->with('blogs' , $post);
    }

索引控制器

 public function index()
    {
        return view('welcome')
        ->with('post' , Post::all())
        ->with('images' , Image::all()->sortByDesc('publish_at')->take(1))
        ->with('categories' , Category::all())
        ->with('top_index' , Post::all()->where('status' , 'publish')->sortByDesc('publish_at')->take(2)->where('status' , 'publish'))
        ->with('recent_posts', Post::all()->take(8)->sortByDesc('publish_at')->splice(2)->where('status', 'publish'));
    }

这工作正常,但是当我在帖子页中使用博客控制器时,其中索引控制器也用于类别,则发生错误 请帮助我

错误 未定义的变量:类别(视图:C:\ xampp \ htdocs \ cms \ resources \ views \ include \ header.blade.php)(视图:C:\ xampp \ htdocs \ cms \ resources \ views \ include \ header.blade。 php)

1 个答案:

答案 0 :(得分:0)

您必须在扩展函数中传递变量,然后按如下所示在子刀片中使用它,

@extends('header', ['categories' => $categories])

您不能直接在子组件刀片中使用标头变量。 希望这可以帮助。

相关问题