Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mohammadmojrian's avatar

laravel 7 blade component public methods not working

I have a problem with my Laravel project in using blade component.

When I define a new component and want to use "Component public Methods" to pass data to "view component blade file", I receive an error, "Undefined variable"

The component has been made by

php artisan make:component testc

has following codes:

component code

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class testc 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.testc');
    }

    public function test()
    {
        return 'test';
    }
}

view component blade file

<div>
    {{ $test }}
</div>

Error

Facade\Ignition\Exceptions\ViewException
Undefined variable: test (View: /../resources/views/components/testc.blade.php)

i try cache:clear view:clear and composer dump before

laravel version : 7.18.0 php version : 7.2.24

0 likes
1 reply
PSchuBu's avatar

I think your problem can be solved by PascalCase your Class and Blade name:

class Testc ... and for your view components.Testc

it‘s not well documented but solves the issue.

Greetings

Please or to participate in this conversation.