```
You may use namespace syntax or dot-notation to create your components in sub-directories. For example, the following commands will create a `CreatePost` component in the `Posts` sub-directory:
```shell
php artisan make:livewire Posts\\CreatePost
php artisan make:livewire posts.create-post
```
### Inline components
If your component is fairly small, you may want to create an _inline_ component. Inline components are single-file Livewire components whose view template is contained directly in the `render()` method rather than a separate file:
```php
{{-- Your Blade template goes here... --}}
HTML;
}
}
```
You can create inline components by adding the `--inline` flag to the `make:livewire` command:
```shell
php artisan make:livewire CreatePost --inline
```
### Omitting the render method
To reduce boilerplate in your components, you can omit the `render()` method entirely and Livewire will use its own underlying `render()` method, which returns a view with the conventional name corresponding to your component:
```php
Title: "{{ $title }}"
```
The rendered output of this component would be:
```blade
Title: "Post title..."
```
### Sharing additional data with the view
In addition to accessing properties from the view, you can explicitly pass data to the view from the `render()` method, like you might typically do from a controller. This can be useful when you want to pass additional data without first storing it as a property—because properties have [specific performance and security implications](/docs/properties#security-concerns).
To pass data to the view in the `render()` method, you can use the `with()` method on the view instance. For example, let's say you want to pass the post author's name to the view. In this case, the post's author is the currently authenticated user:
```php
with([
'author' => Auth::user()->name,
]);
}
}
```
Now you may access the `$author` property from the component's Blade view:
```blade
Title: {{ $title }}
Author: {{ $author }}
```
### Adding `wire:key` to `@foreach` loops
When looping through data in a Livewire template using `@foreach`, you must add a unique `wire:key` attribute to the root element rendered by the loop.
Without a `wire:key` attribute present within a Blade loop, Livewire won't be able to properly match old elements to their new positions when the loop changes. This can cause many hard to diagnose issues in your application.
For example, if you are looping through an array of posts, you may set the `wire:key` attribute to the post's ID:
```blade
@foreach ($posts as $post)
@endforeach
```
### Binding inputs to properties
One of Livewire's most powerful features is "data binding": the ability to automatically keep properties in-sync with form inputs on the page.
Let's bind the `$title` property from the `CreatePost` component to a text input using the `wire:model` directive:
```blade
```
Any changes made to the text input will be automatically synchronized with the `$title` property in your Livewire component.
> [!warning] "Why isn't my component live updating as I type?"
> If you tried this in your browser and are confused why the title isn't automatically updating, it's because Livewire only updates a component when an "action" is submitted—like pressing a submit button—not when a user types into a field. This cuts down on network requests and improves performance. To enable "live" updating as a user types, you can use `wire:model.live` instead. [Learn more about data binding](/docs/properties#data-binding).
Livewire properties are extremely powerful and are an important concept to understand. For more information, check out the [Livewire properties documentation](/docs/properties).
## Calling actions
Actions are methods within your Livewire component that handle user interactions or perform specific tasks. They're often useful for responding to button clicks or form submissions on a page.
To learn more about actions, let's add a `save` action to the `CreatePost` component:
```php
$this->title
]);
return redirect()->to('/posts')
->with('status', 'Post created!');
}
public function render()
{
return view('livewire.create-post');
}
}
```
Next, let's call the `save` action from the component's Blade view by adding the `wire:submit` directive to the `
```
When the "Save" button is clicked, the `save()` method in your Livewire component will be executed and your component will re-render.
To keep learning about Livewire actions, visit the [actions documentation](/docs/actions).
## Rendering components
There are two ways to render a Livewire component on a page:
1. Include it within an existing Blade view
2. Assign it directly to a route as a full-page component
Let's cover the first way to render your component, as it's simpler than the second.
You can include a Livewire component in your Blade templates using the `` syntax:
```blade
```
> [!warning] You must use kebab-case
> As you can see in the snippet above, you must use the _kebab-cased_ version of the component name. Using the _StudlyCase_ version of the name (``) is invalid and won't be recognized by Livewire.
### Passing data into components
To pass outside data into a Livewire component, you can use attributes on the component tag. This is useful when you want to initialize a component with specific data.
To pass an initial value to the `$title` property of the `CreatePost` component, you can use the following syntax:
```blade
```
If you need to pass dynamic values or variables to a component, you can write PHP expressions in component attributes by prefixing the attribute with a colon:
```blade
```
Data passed into components is received through the `mount()` lifecycle hook as method parameters. In this case, to assign the `$title` parameter to a property, you would write a `mount()` method like the following:
```php
title = $title;
}
// ...
}
```
In this example, the `$title` property will be initialized with the value "Initial Title".
You can think of the `mount()` method as a class constructor. It runs on the initial load of the component, but not on subsequent requests within a page. You can learn more about `mount()` and other helpful lifecycle hooks within the [lifecycle documentation](/docs/lifecycle-hooks).
To reduce boilerplate code in your components, you can alternatively omit the `mount()` method and Livewire will automatically set any properties on your component with names matching the passed in values:
```php
[!warning] These properties are not reactive by default
> The `$title` property will not update automatically if the outer `:title="$initialValue"` changes after the initial page load. This is a common point of confusion when using Livewire, especially for developers who have used JavaScript frameworks like Vue or React and assume these "parameters" behave like "reactive props" in those frameworks. But, don't worry, Livewire allows you to opt-in to [making your props reactive](/docs/nesting#making-child-props-reactive).
## Full-page components
Livewire allows you to assign components directly to a route in your Laravel application. These are called "full-page components". You can use them to build standalone pages with logic and views, fully encapsulated within a Livewire component.
To create a full-page component, define a route in your `routes/web.php` file and use the `Route::get()` method to map the component directly to a specific URL. For example, let's imagine you want to render the `CreatePost` component at the dedicated route: `/posts/create`.
You can accomplish this by adding the following line to your `routes/web.php` file:
```php
use App\Livewire\CreatePost;
Route::get('/posts/create', CreatePost::class);
```
Now, when you visit the `/posts/create` path in your browser, the `CreatePost` component will be rendered as a full-page component.
### Layout files
Remember that full-page components will use your application's layout, typically defined in the `resources/views/components/layouts/app.blade.php` file.
Ensure you have created a Blade file at this location and included a `{{ $slot }}` placeholder:
```blade
{{ $title ?? 'Page Title' }}
{{ $slot }}
```
#### Global layout configuration
To use a custom layout across all your components, you can set the `layout` key in `config/livewire.php` to the path of your custom layout, relative to `resources/views`. For example:
```php
'layout' => 'layouts.app',
```
With the above configuration, Livewire will render full-page components inside the layout file: `resources/views/layouts/app.blade.php`.
#### Per-component layout configuration
To use a different layout for a specific component, you can place Livewire's `#[Layout]` attribute above the component's `render()` method, passing it the relative view path of your custom layout:
```php
layout()` method in the component's `render()` method:
```php
public function render()
{
return view('livewire.create-post')
->layout('layouts.app'); // [tl! highlight]
}
```
### Setting the page title
Assigning unique page titles to each page in your application is helpful for both users and search engines.
To set a custom page title for a full-page component, first, make sure your layout file includes a dynamic title:
```blade
{{ $title ?? 'Page Title' }}
```
Next, above your Livewire component's `render()` method, add the `#[Title]` attribute and pass it your page title:
```php
title()` fluent method in the component's `render()` method:
```php
public function render()
{
return view('livewire.create-post')
->title('Create Post'); // [tl! highlight]
}
```
### Accessing route parameters
When working with full-page components, you may need to access route parameters within your Livewire component.
To demonstrate, first, define a route with a parameter in your `routes/web.php` file:
```php
use App\Livewire\ShowPost;
Route::get('/posts/{id}', ShowPost::class);
```
Here, we've defined a route with an `id` parameter which represents a post's ID.
Next, update your Livewire component to accept the route parameter in the `mount()` method:
```php
post = Post::findOrFail($id);
}
public function render()
{
return view('livewire.show-post');
}
}
```
In this example, because the parameter name `$id` matches the route parameter `{id}`, if the `/posts/1` URL is visited, Livewire will pass the value of "1" as `$id`.
### Using route model binding
Laravel's route model binding allows you to automatically resolve Eloquent models from route parameters.
After defining a route with a model parameter in your `routes/web.php` file:
```php
use App\Livewire\ShowPost;
Route::get('/posts/{post}', ShowPost::class);
```
You can now accept the route model parameter through the `mount()` method of your component:
```php
post = $post;
}
public function render()
{
return view('livewire.show-post');
}
}
```
Livewire knows to use "route model binding" because the `Post` type-hint is prepended to the `$post` parameter in `mount()`.
Like before, you can reduce boilerplate by omitting the `mount()` method:
```php
Oops! Page not found.
Sorry, an error has occured, Requested page not found!