Zr1 Vette
Zr1 Vette

Reputation: 31

laravel View Components

I have a view component that is in the components folder but is in a sub folder named models I have always accessed it with the following tag

<x-models.models></x-models.models>

It has always worked fine but now I need to pass data to it. I understand how the App/View/Components work but since the blade is in a sub folder I am not able to figure out how to create the Component. Any ideas?

Upvotes: 2

Views: 1226

Answers (1)

John Lobo
John Lobo

Reputation: 15319

Using class you can return subcomponent view

<?php

namespace App\View\Components;

use Illuminate\View\Component;

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

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.models.models');


    }
}

Also without class also we can pass data to sub component models

For example i have created a model in views/components/bootstrap/modal.blade.php

@props(['title' => '','footer'=>'','id'=>''])

    <div {{ $attributes->merge(['class' => 'modal fade commonModal']) }}  id="{{$id}}" tabindex="-1" role="dialog"    aria-labelledby="{{$id}}" aria-hidden="true"  >
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">{{$title}}</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body ">
                  {{$slot}}
                </div>
                <div class="modal-footer">
                 {{$footer}}
    
                </div>
            </div>
        </div>
    </div>

and while accessing

 <x-bootstrap.modal id="modalId" class="modelClass">
        <x-slot name="title">
           Title
        </x-slot>
       
     whaterve content
    </x-bootstrap.modal>

Upvotes: 1

Related Questions