framework: component class is not receiving any values

  • Laravel Version: 8.12
  • PHP Version: PHP Version 7.2.34-9+ubuntu20.04.1+deb.sury.org+1
  • Database Driver & Version: mysqlnd 5.0.12-dev - 20150407 - $Id: 3591daad22de08524295e1bd073aceeff11e6579 $

Description:

it seems like the same bug as this issue: https://github.com/laravel/framework/issues/31919

no props or variables are passed to the component class. the attributes appear in the template but not the variables.

I’ve tried proposed solutions from #31919 but it did not work for me…

Steps To Reproduce:

my component class

<?php

namespace App\View\Components;

use Illuminate\View\Component;

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

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|string
     */
    public function render()
    {
        var_dump('render called');
        var_dump($this); //everything is null here
        return view('components.form.checkboxes');
    }
}

registered in AppServiceProvider:

public function boot()
    {
        
        Blade::component('checkboxes', Checkboxes::class);
    }

this is the component template:

@php
var_dump(@$data);
$data = $data ?? [];
$name = @$attributes['name'] ?? "name";
$id = @$attributes['id'] ?? 'id';
$fieldName = @$attributes['field'];
$inline= @$attributes['inline'] ? 'flex' : '';
@endphp
<ul class="<?= $inline ?>">
    @foreach ($data as $item)
    @php
    $fieldId = $fieldName."_".$item->id; //TODO not working o
    @endphp
    <li>
        <?= Form::checkbox($fieldName.'[]', $item->$id, null, ['id'=>$fieldId]) ?>
        <label for="<?=$fieldId ?>" class="font-bold mr-2"><?=$item->name ?></label>
    <li>
        @endforeach
</ul>

and this is how I call it in another template:

<x-checkboxes field="roles" name="name" value="id" :data="$roles" inline="true">
                </x-checkboxes>

cheers, dan

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Works for me. Try clearing your views php artisan view:clear

@danielzzz you’re not instantiating the properties. See the example here: https://laravel.com/docs/8.x/blade#passing-data-to-components

    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct($koko, $zaza)
    {
        $this->koko = $koko;
        $this->zaza = $zaza;
    }