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] This package is in beta and its API may change.
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.
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.
datastar()->getFragments()
Returns a @get()
action request to render and automatically merge the fragments contained in a view at the given path. The view should contain one or more fragments to be merged.
Blade Directives
@mergefragments
Merges one or more fragments into the DOM.
@removefragments
Removes one or more HTML fragments that match the provided selector from the DOM.
@mergesignals
Updates the signals with new values.
@removesignals
Removes signals that match one or more provided paths.
@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
mergeFragments()
Merges one or more fragments into the DOM.
removeFragments()
Removes one or more HTML fragments that match the provided selector from the DOM.
mergeSignals()
Updates the signals with new values.
removeSignals()
Removes signals that match one or more provided paths.
executeScript()
Executes JavaScript in the browser.
location()
Redirects the browser by setting the location to the provided URI.
renderDatastarView()
Renders a Datastar view.
Signals
When working with signals, either in views rendered by the Datastar controller or by calling $this->getSignals()
, you are working with a Signals model, which provides a simple way to manage signals.
[!NOTE] Signals updates cannot be wrapped in
{% mergefragment %}
tags, since each update creates a server-sent event which will conflict with the fragment’s contents.
Created by PutYourLightsOn.
All versions of laravel-datastar with dependencies
laravel/framework Version ^11.0
starfederation/datastar-php Version 1.0.0-beta.17