1

I've just migrated a project that was working great on my localhost to a shared hosting and my components suddently are not getting the methods that i gave them and i'm getting errors in my views like so :

Undefined variable: CatPromo

this is my Component :

<?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;
}
}

Update : I removed the App\View\Components\promo.php to see if it can help me by throwing an error and it seems that he don't even detect the controller.

Evilbleezard
  • 69
  • 1
  • 14
  • May be small letter and capital letter is the problem. Did you check text transform? – STA Jul 04 '20 at 18:01
  • The component controller is not being called i can see that Through `{{ dd(get_defined_vars()['__data']) }}` and are using `Illuminate\View\AnonymousComponent` instead of `App\View\Components\Promo` like it was supposed to do – Evilbleezard Jul 05 '20 at 08:36
  • Did you find a solution to this? I have the same issue in production. "Works on my machine" though... – Michael Krøyserth-Simsø May 12 '21 at 21:48

1 Answers1

1

The documentation says: You should define the component's required data in its class constructor.

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

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

And in blade template:

@foreach($CatPromo() as $key => $Categorie)
Beller
  • 828
  • 1
  • 6
  • 19
  • This has been updated since the name of the method can be used as variable to get the data, like i'm doing in my localhost. but to be sure i've tryed this synthax but still not working. – Evilbleezard Jul 04 '20 at 17:35
  • i include my component with a `` and i do a simple `@foreach($CatPromo as $key =>$Categorie` – Evilbleezard Jul 04 '20 at 18:37
  • It appears that Laravel is not even reading the controller at all. – Evilbleezard Jul 04 '20 at 18:42
  • Just tryed it, still not working `CatPromo` i've tryed to display all my variables with `{{ dd(get_defined_vars()['__data']) }}` i don't even see a the same variables as in my localhost. – Evilbleezard Jul 04 '20 at 19:04
  • Could you try with camelCase? Because documentation mentions that. – Beller Jul 04 '20 at 19:11
  • they have similar examples of my issue here : https://github.com/laravel/framework/issues/31919 – Evilbleezard Jul 04 '20 at 20:43
  • The component controller is not being called i can see that Through `{{ dd(get_defined_vars()['__data']) }}` and are using `Illuminate\View\AnonymousComponent` instead of `App\View\Components\Promo` like it was supposed to do, so what ever i do in the controller it will not be detected. – Evilbleezard Jul 05 '20 at 08:43