PHPackages                             intrfce/laravel-inertia-components - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. intrfce/laravel-inertia-components

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

intrfce/laravel-inertia-components
==================================

v0.1.1(1y ago)125proprietaryPHPPHP ^8.2.0

Since Aug 31Pushed 1y ago1 watchersCompare

[ Source](https://github.com/intrfce/laravel-inertia-components)[ Packagist](https://packagist.org/packages/intrfce/laravel-inertia-components)[ RSS](/packages/intrfce-laravel-inertia-components/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (2)Used By (0)

Laravel Inertia Components
==========================

[](#laravel-inertia-components)

Warning

This package is in a pre-release state and the API may change.

This package allows you to create Livewire-style class components for your InertiaJS applications.

Features:
---------

[](#features)

- ✅ Define HTTP-named resourceful style methods for `show`/`store`/`update`/`destroy` right in the same class.
- ✅ Any `public` properties or methods pass data back to your components.
- ✅ Attributes to mark public methods as `#[Lazy]` or `#[Always]`
- ✅ Create methods that auto-wire their own routes by using the `#[PostAction]`, `#[GetAction]` etc Attributes.
- ⌛ Create components with `php artisan make:inertia`
- ✅ `Route::inertia` helper.

Why?
----

[](#why)

- Better organisation and encapsulation of code.
- Reduce clutter in your route files.

HTTP/Resourceful Methods.
-------------------------

[](#httpresourceful-methods)

The base class reserves four `public` method names to use and their corresponding HTTP methods:

Method NameHTTP MethodshowGETstorePOSTupdatePATCH/PUTdestroyDELETEEach of these methods behaves just like a controller method should, so you can inject route parameters and dependencies as needed:

```
public function show(Request $request, string $id, ModelName $model): array {}
```

### Returning data from `show()`

[](#returning-data-from-show)

The `show` method is used when your route is hit with a `GET` request.

If you return an `array` from this method, it will be merged with the rest of the public properties and methods, you can also use the usual `Inertia::lazy` and `Inertia::always` closures from here too.

Autowiring HTTP Methods.
------------------------

[](#autowiring-http-methods)

If you need to call another route that's non-RESTful or tangential to the resource you're showing, you can use autowiring methods, these will register the route for you with either an automatic or user-given URL.

For example, showing a list of blog posts on your `show` route, but want the ability to send a quick AJAX request to toggle the post as published or unpublished?

Setup a function in your component, and add the appropriate HTTP method action attribute to it:

```
#[PatchAction(url:'/{post}/toggle-published')]
function togglePublished(BlogPost $post) {
    $post->published = !$post->published;
    $post->save();
    return redirect()->back();
}
```

Assuming your component was registered with a path like `/posts`, this would automatically register a route at `POST posts/{post}/toggle-published` for you.

> Note: if you use the `#[GetAction]` attribute, you shouldn't return an Inertia::response() from it - if you want to do that, use another fully registered route instead.

Full (WIP) Example:
-------------------

[](#full-wip-example)

```
