Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with passing parameter to component

Tags:

laravel

Reading how to make components here https://laravel.com/docs/7.x/blade#components in my Laravel Framework 7.6.2 app I got error :

Unresolvable dependency resolving [Parameter #0 [ <required> $is_auto_hide ]] in class App\View\Components\AppBackendHeader (View:

for my first var defined in the component app/View/Components/AppBackendHeader.php :

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class AppBackendHeader extends Component
{

    public $is_auto_hide;

    public $message;
    public function __construct($is_auto_hide, $message)
    {
        $this->is_auto_hide = $is_auto_hide;
        $this->message = $message;
    }
    public function render()
    {
        return view('components.app-backend-header');
    }
}


calling this component from blade page : 
        <x-app-backend-header is_auto_hide="true" message="message 1234" />


In app/Providers/AppServiceProvider.php I added line :

    public function boot()
    {
        Blade::component('app-backend-header', AppBackendHeader::class);
...

Calling the template I pass is_auto_hide parameter and it does not looks like misspelling...

I tried to run commands:

$ composer dump-autoload 
$ php artisan view:clear
$ php artisan config:cache

and it did not help... What is the problem ?

Thanks!

like image 747
Petro Gromovo Avatar asked Oct 21 '25 15:10

Petro Gromovo


1 Answers

According to the documentation:

Component constructor arguments should be specified using camelCase, while kebab-case should be used when referencing the argument names in your HTML attributes.

This means you need:

public function __construct($isAutoHide, $message)
{
    $this->is_auto_hide = $isAutoHide;
    $this->message = $message;
}

and you should refer to it as:

 <x-app-backend-header is-auto-hide="true" message="message 1234" />
like image 78
apokryfos Avatar answered Oct 23 '25 07:10

apokryfos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!