Download the PHP package putyourlightson/laravel-datastar without Composer
On this page you can find all versions of the php package putyourlightson/laravel-datastar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download putyourlightson/laravel-datastar
More information about putyourlightson/laravel-datastar
Files in putyourlightson/laravel-datastar
Package laravel-datastar
Short Description A reactive hypermedia framework for Laravel.
License mit
Informations about the package laravel-datastar
Datastar Package for Laravel
A reactive hypermedia framework for Laravel.
[!WARNING] Updating from a previous version? View the release notes.
This package integrates the Datastar hypermedia framework with Laravel, allowing you to create reactive frontends driven by Blade views or controllers. It aims to replace the need for front-end frameworks such as React, Vue.js and Alpine.js + htmx, and instead lets you manage state and use logic from your Laravel backend.
Use-cases:
- Live search and filtering
- Loading more elements / Infinite scroll
- Paginating, ordering and filtering lists
- Submitting forms and running actions
- Pretty much anything to do with reactive front-ends
License
This package is licensed for free under the MIT License.
Requirements
This package requires Laravel 11.0.0 or later.
Installation
Install manually using composer, then run the artisan vendor:publish --tag=public
command to publish the public assets.
Overview
The Datastar package for Laravel allows you to handle backend requests by sending SSE events using using controllers. The former requires less setup and is more straightforward, while the latter provides more flexibility.
Here’s a trivial example that toggles some backend state using the Blade view datastar/toggle.blade.php
to handle the request.
Usage
Start by reading the Getting Started guide to learn how to use Datastar on the frontend. The Datastar package for Laravel only handles backend requests.
[!NOTE] The Datastar VSCode extension and IntelliJ plugin have autocomplete for all
data-*
attributes.
When working with signals, note that you can convert a PHP array into a JSON object using the json_encode
function.
Datastar Helper
The datastar()
helper function is available in Blade views and returns a Datastar
helper that can be used to generate action requests to the Datastar controller. The Datastar controller can either render a view, run a controller action, or call a route, each of which respond by sending an event stream containing zero or more SSE events.
Signals are also sent as part of the request, and are made available in Datastar views using the $signals
variable.
datastar()->view()
Returns a @get()
action request to render a view. The value should be a dot-separated path to a Blade view.
Variables can be passed in as a second argument, that will be available in the rendered view.
{warning} Variables are tamper-proof yet visible in the source code in plain text, so you should avoid passing in any sensitive data. Only primitive data types can be used as variables: strings, numbers, booleans and arrays. Objects and models cannot be used.
Options can be passed into the @get()
action using a third argument.
datastar()->action()
Returns a @post()
action request to run a controller action. The value should be an array with a controller class name as the first value and an action name as the second. A CSRF token is automatically generated and sent along with the request.
Params can be passed in as a second argument, that will be available as arguments to the controller action
{warning} Params are tamper-proof yet visible in the source code in plain text, so you should avoid passing in any sensitive data. Only primitive data types can be used as params: strings, numbers, booleans and arrays. Objects and models cannot be used. Route-model binding works with controller actions.
Options can be passed into the @get()
action using a third argument.
datastar()->get()
Returns a @get()
action request that calls a route. The value must be a defined route.
Options can be passed into the @get()
action using a second argument.
datastar()->post()
Works the same as datastar()->get()
but returns a @post()
action request that calls a route. A CSRF token is automatically generated and sent along with the request.
datastar()->put()
Works the same as datastar()->post()
but returns a @put()
action request that calls a route.
datastar()->patch()
Works the same as datastar()->post()
but returns a @patch()
action request that calls a route.
datastar()->delete()
Works the same as datastar()->post()
but returns a @delete()
action request that calls a route.
Blade Directives
Datastar Blade directives can patch and remove elements, patch signals, execute JavaScript and redirect the browser.
Patch Elements
The @patchelements
directive allows you to patch elements into the DOM.
This will swap the elements with the IDs results
and search
into the DOM. Note that elements with those IDs must already exist in the DOM, unless a mode is specified (see below).
Element Patch Options
Elements are patched into the DOM based on element IDs, by default. It’s possible to pass other modes and other element patch options in as an argument.
Automatic Element Patching
Any elements output in a Datastar template (outside any @patchelements
tags) will be automatically wrapped in a @patchelements
directive. This makes it possible to write your views in a way that makes them more reusable.
The view above is the equivalent of writing:
While automatic element patching is convenient, it is less explicit and more restrictive (since element patch options cannot be used), so should only be used when appropriate.
Remove Elements
Elements can be removed from the DOM using the @removeelements
directive, which accepts a CSS selector.
Patch Signals
The @patchsignals
directive allows you to patch signals into the frontend signals.
{note} Signals patches cannot be wrapped in
@patchelements
directives, since each patch creates a server-sent event which will conflict with the element’s contents.
Signal Patch Options
It’s possible to pass signal patch options in as a second argument.
Executing JavaScript
The @executescript
directive allows you to send JavaScript to the browser to be executed on the front-end.
Execute Script Options
It’s possible to pass execute script options in as an argument. They are applied to the <script>
tag that is appended to the DOM.
Redirecting
The @location
directive allows you to redirect the page by updating window.location
on the front-end.
Location Options
It’s possible to pass location options in as a second argument. They are applied to the <script>
tag that is appended to the DOM.
Using Controllers
You can send SSE events from your own controller using the sse()
helper. No routes are required, as Datastar will handle routing to the controller action you specify when using the Datastar helper.
Controller actions must return a StreamedResponse
created using the getEventStream()
method.
sse()
Helper
The sse()
helper provides methods to patch elements, patch signals, execute scripts, redirect the browser and render Datastar views.
patchElements()
Patches elements into the DOM. Accepts element patch options as an optional second argument.
removeElements()
Removes elements that match the provided selector from the DOM.
patchSignals()
Patches signals into the frontend. Accepts signal patch options as an optional second argument.
executeScript()
Executes JavaScript in the browser. Accepts execute script options as an optional second argument, which are applied to the <script>
tag that is appended to the DOM.
location()
Redirects the browser by setting the location to the provided URI. Accepts location options as an optional second argument, which are applied to the <script>
tag that is appended to the DOM.
renderView()
Renders a Datastar view. Accepts the view path as the first argument and an optional array of variables as the second argument. The Blade view should output Datastar directives.
Signals
Signals can be accessed within views rendered by Datastar using the signals variable, which is an array of signals received by the request that is automatically injected into the template.
If you ever need to read the signals in a request that is not handled by the Datastar package, you can do so as follows.
[!NOTE] Signal patches cannot be wrapped in
@patchelements
directives, since each update creates a server-sent event which will conflict with the element’s contents.
Created by PutYourLightsOn.
All versions of laravel-datastar with dependencies
laravel/framework Version >=11.0
starfederation/datastar-php Version ^1.0.0-RC.3