Laravel - Blade Templates - Components
Components and slots provide similar benefits to sections, layouts, and includes; however, some may find the mental model of components and slots easier to understand. There are two approaches to writing components: class based components and anonymous components.
To create a class based component, you may use the make:component
Artisan command. To illustrate how to use components, we will create a simple Alert
component. The make:component
command will place the component in the App\View\Components
directory:
php artisan make:component Alert
The make:component
command will also create a view template for the component. The view will be placed in the resources/views/components
directory. When writing components for your own application, components are automatically discovered within the app/View/Components
directory and resources/views/components
directory, so no further component registration is typically required.
You may also create components within subdirectories:
php artisan make:component Forms/Input
The command above will create an Input
component in the App\View\Components\Forms
directory and the view will be placed in the resources/views/components/forms
directory.