alirezaashrafi
alirezaashrafi

Reputation: 153

Laravel 8 components @props directive is not working

ErrorException Undefined variable: label (View: resources/views/components/forms/select.blade.php)

How can I fix this issue? I did everything to solve this problem but I could not find a solution. I have been solving this problem for 5 days!

I upgraded Laravel 7 to 8.

Now Laravel: v8.26.1

<x-forms.select name="subject" label="Subjects" value="test">
    @foreach($user->subjects as $subject)
        <option value="{{$subject->id}}">{{$subject->name}}</option>
    @endforeach
</x-forms.select>
// app/View/Components/Forms/Select.php

<?php

namespace App\View\Components\Forms;

use Illuminate\View\Component;

class Select extends Component
{
  
    public function render()
    {
        return view('components.forms.select');
    }
}
// resources/views/components/forms/select.blade.php

@props(['label'])
{{$label}}

<label class="form-label">{{ $attributes->get('label') }}</label>

<select class="form-select" aria-label="Default select" {{$attributes}}>
    {{ $slot }}
</select>

Laravel 8 components @props directive is not working:

image

Upvotes: 2

Views: 12452

Answers (1)

alirezaashrafi
alirezaashrafi

Reputation: 153

Thank you, Taylor Otwell

@props is only for anonymous components that do not have a class. If you are using a class, consult this documentation: Passing Data To Components | Laravel

Upvotes: 9

Related Questions