PHP code example of putyourlightson / laravel-datastar
1. Go to this page and download the library: Download putyourlightson/laravel-datastar library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
putyourlightson / laravel-datastar example snippets
{{-- main.blade.php --}}
<div data-signals-enabled="false">
<div data-text="$enabled ? 'ON' : 'OFF'"></div>
<button data-on-click="{{ datastar()->view('datastar.toggle') }}">
<span id="button-text">Enable</span>
</button>
</div>
{{-- datastar/toggle.blade.php --}}
@php
$enabled = $signals['enabled'] ?? false;
@endphp
@patchsignals(['enabled' => $enabled])
@patchelements
<span id="button-text">
{{ $enabled ? 'Disable' : 'Enable' }}
</span>
@endpatchelements
{{-- main.blade.php --}}
@php
$signals = ['foo' => 1, 'bar' => 2];
@endphp
<div data-signals="{{ json_encode($signals) }}"></div>
// Sends a `GET` request that renders a Blade view
{{ datastar()->view('path.to.view') }}
// Sends a `GET` request that renders a Blade view
{{ datastar()->view('path.to.view', ['foo' => 'bar']) }}
// Sends a `GET` request that renders a Blade view
{{ datastar()->view('path.to.view', ['foo' => 'bar'], ['contentType' => 'form']) }}
// Sends a `POST` request that runs a controller action
{{ datastar()->action(['MyController', 'update']) }}
// Sends a `POST` request that runs a controller action
{{ datastar()->action(['MyController', 'update'], ['foo' => 'bar']) }}
// Sends a `POST` request that runs a controller action
{{ datastar()->action(['MyController', 'update'], ['foo' => 'bar'], ['contentType' => 'form']) }}
// Sends a `GET` request to a route
{{ datastar()->get('/uri') }}
// Sends a `GET` request to a route
{{ datastar()->get('/uri', ['contentType' => 'form']) }}
// Sends a `POST` request to a route
{{ datastar()->post('/uri') }}
// Sends a `PUT` request to a route
{{ datastar()->put('/uri') }}
// Sends a `PATCH` request to a route
{{ datastar()->patch('/uri') }}
// Sends a `DELETE` request to a route
{{ datastar()->delete('/uri') }}
{{-- main.blade.php -}}
<div id="results"></div>
<div id="search">
<button data-on-click="{{ datastar()->get('datastar.search') }}">
Search
</button>
</div>
{{-- datastar/search.blade.php -}}
@patchelements
<div id="results">
...
</div>
@endpatchelements
@patchelements
<div id="search">
Search complete!
</div>
@endpatchelements
@patchelements(['selector' => '#list', 'mode' => 'append'])
<li>A new list item</li>
@endpatchelements
{{-- datastar/search.blade.php -}}
<div id="results"></div>
{{-- datastar/search.blade.php -}}
@patchelements
<div id="results"></div>
@endpatchelements
@removeelements('#list')
{{- Sets the value of the `username` signal. -}}
@patchsignals(['username' => 'johnny'])
{{- Sets multiple signal values using an array of key-value pairs. -}}
@patchsignals(['username' => 'bobby', 'success' => true])
{{- Removes the `username` signal by setting it to `null`. -}}
@patchsignals(['username' => null])
@patchsignals(['username' => 'johnny'], ['onlyIfMissing' => true])
@executescript
alert('Username is valid');
@endexecutescript
@executescript(['autoRemove' => true, 'attributes' => ['defer' => true]])
alert('Username is valid');
@endexecutescript
@location('/guide')
@location('/guide', ['autoRemove' => true, 'attributes' => ['defer' => true]])
{{-- main.blade.php --}}
// Sends a `POST` request that runs a controller action
{{ datastar()->action(['MyController', 'update']) }}
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\StreamedResponse;
class MyController extends Controller
{
public function index(): StreamedResponse
{
$signals = sse()->readSignals();
sse()->patchSignals(['enabled' => $signals['enabled'] ? false : true]);
sse()->patchElements('
<span id="button-text">' . ($signals['enabled'] ? 'Enable' : 'Disable') . '</span>
');
return sse()->getEventStream();
}
public function view(): StreamedResponse
{
sse()->renderView('path.to.view');
return sse()->getEventStream();
}
}
sse()->patchElements('<div id="new-element">New element</div>');
sse()->removeElements('#list');
sse()->patchSignals(['foo' => 1, 'bar' => 2]);
sse()->executeScript('alert("Hello, world!")');
sse()->location('/guide');
sse()->renderView('datastar.toggle', ['enabled' => true]);
<input data-bind-username>
<button data-on-click="{{ datastar()->get('path.to.view') }}">
Check
</button>
@php
$username = $signals['username'];
@endphp
@php
$signals = sse()->readSignals();
@endphp