Php 如何在2页上显示类别
我需要一些帮助,目前在2页的所有类别。 现在我在1页中显示它们。我的问题是如何在其他页面中也显示它们? 这是控制器:Php 如何在2页上显示类别,php,laravel,laravel-5,laravel-5.2,laravel-5.1,Php,Laravel,Laravel 5,Laravel 5.2,Laravel 5.1,我需要一些帮助,目前在2页的所有类别。 现在我在1页中显示它们。我的问题是如何在其他页面中也显示它们? 这是控制器: class ShopController extends MainController { public function categories(){ self::$data['categories']=Categorie::all()->toArray(); self::$data['title']=self::$da
class ShopController extends MainController
{
public function categories(){
self::$data['categories']=Categorie::all()->toArray();
self::$data['title']=self::$data['title'].'| Shop Categories';
return view('content.categories', self::$data);
}
如果我试图使用extends并从“content.categories”页面中获得它,它会说$categories是未定义的。
(因此它只适用于content.categories)如果您坚持这样做,您可以在
content.categories
视图文件中使用此代码段
@extends('your-layout', ['categories' => $categories])
更改控制器,如下所示
class ShopController extends MainController
{
public function __construct()
{
// define variable in your construct
$this->data = array(
'title' => 'Your Title'
);
}
public function categories(){
$this->data['categories']=Categorie::all()->toArray();
$this->data['title']=$this->data['title'].'| Shop Categories';
return view('content.categories', $this->data);
}
在您的视图文件content/categories.blade.php
使用
$categories
获取数据您是否试图访问由content.categories
扩展的布局中的类别?是的,但它告诉我$categories是未定义的返回视图('content.categories')->with categories(self:$data)代码>!!为什么要将$data
变量设置为静态?您是否尝试过在不使$data
静态的情况下使用它?它仍然显示未定义的变量:categories(视图:categories.blade.php)