簡體   English   中英

未定義的變量:類別(查看:在Laravel中)如何解決此問題

[英]Undefined variable: categories (View: in Laravel) How to Solve This

我在使用laravel 5.8博客的帖子頁的問題 在首頁上,它的工作效果與帖子頁面的表單代碼相同,但是我創建了錯誤。 idk為什么我有首頁路由

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)

您必須在擴展函數中傳遞變量,然后按如下所示在子刀片中使用它,

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

您不能直接在子組件刀片中使用標頭變量。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM