首页 > 解决方案 > Laravel 组件没有在共享主机中获取我的方法

问题描述

我刚刚将一个在我的本地主机上运行良好的项目迁移到共享主机,我的组件突然没有得到我给它们的方法,我的视图中出现了如下错误:

Undefined variable: CatPromo

这是我的组件:

<?php

namespace App\View\Components;

use Illuminate\View\Component;
use App\Categories;

class promo extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

/**
 * Get the view / contents that represent the component.
 *
 * @return \Illuminate\View\View|string
 */
public function render()
{
    return view('components.promo');
}

public function CatPromo()
{
    $Categories = Categories::all();
    return $Categories;
}
}

更新:我删除了App\View\Components\promo.php,看看它是否可以通过抛出错误来帮助我,似乎他甚至没有检测到控制器。

标签: laravelviewcomponents

解决方案


文档说:您应该在其类构造函数中定义组件所需的数据。

    public function __construct($CatPromo)
    {
        // use as variable
        $this->CatPromo = $CatPromo;
    }

    // use as method
    public function CatPromo()
    {
      $Categories = Categories::all();
      return $Categories;
    }

在刀片模板中:

@foreach($CatPromo() as $key => $Categorie)

推荐阅读