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 the beta? 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 renders a view containing one or Signals are also sent as part of the request, and are made available in Datastar views using the $signals
variable.
datastar()->get()
Returns a @get()
action request to render a view at the given path. The value can be a file path or a dot-separated path to a Blade view.
Variables can be passed into the view using a second argument. Any variables passed in will become available in the rendered view. Variables are tamper-proof yet visible in the source code in plain text, so you should avoid passing in any sensitive data.
Options can be passed into the @get()
action using a third argument.
datastar()->post()
Works the same as datastar()->get()
but returns a @post()
action request to render a view at the given path. 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.
datastar()->patch()
Works the same as datastar()->post()
but returns a @patch()
action request.
datastar()->delete()
Works the same as datastar()->post()
but returns a @delete()
action request.
Blade Directives
@patchelements
Patches elements into the DOM.
@removeelements
Removes elements that match the provided selector from the DOM.
@patchsignals
Patches signals into the frontend.
@executescript
Executes JavaScript in the browser.
@location
Redirects the browser by setting the location to the provided URI.
Using Controllers
You can send SSE events using your own controller instead of the Datastar controller by using the DatastarEventStream
trait. Return the getStreamedResponse()
method, passing a callable into it that sends zero or more SSE events using methods provided.
DatastarEventStream Trait
patchElements()
Patches elements into the DOM.
removeElements()
Removes elements that match the provided selector from the DOM.
patchSignals()
Patches signals into the frontend.
executeScript()
Executes JavaScript in the browser.
location()
Redirects the browser by setting the location to the provided URI.
renderDatastarView()
Renders a Datastar view.
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.2