Cannot pass variable to Named Slot in blade Component Laravel 8

I try to pass a String to a blade component, but receive the following error message: Undefined variable $lead (View: path-to-blade-component)

My blade component file (components/school-project.blade.php):

<div class="col-xs-12 col-sm-12 col-md-4">
  <p class="lead">{{ $lead }}</p>
  {{$slot}}
</div>

code in view file:

@extends('layouts.static')
 @section('content')
      <x-school-project>
        <x-slot name="lead">Lead Text</x-slot>
        <!-- Here goes other stuff ($slot) -->
      </x-school-project>
@endsection

I also want to use the $lead variable in a modal in the component therefore I need to use a named slot. Can anyone tell me where the reasons for the error could be. Thanxs!!

1 answer

  • answered 2021-04-21 14:12 Autista_z

    Variable to component you can pass as :name="value"

    So in your case:

    @extends('layouts.static')
     @section('content')
          <x-school-project :lead="'Lead text'">
            <x-slot name="lead">Lead Text</x-slot>
            <!-- Here goes other stuff ($slot) -->
          </x-school-project>
    @endsection