generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e709ee4
commit ac30b6e
Showing
10 changed files
with
173 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: Use with Livewire | ||
weight: 7 | ||
--- | ||
|
||
> Livewire is a full-stack framework for Laravel that makes building dynamic interfaces simple without leaving the comfort of Laravel. | ||
Laravel Data works excellently with [Laravel Livewire](https://laravel-livewire.com). | ||
|
||
You can use a data object as one of the properties of your Livewire component as such: | ||
|
||
```php | ||
class Song extends Component | ||
{ | ||
public SongData $song; | ||
|
||
public function mount(int $id) | ||
{ | ||
$this->song = SongData::from(Song::findOrFail($id)); | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire.song'); | ||
} | ||
} | ||
``` | ||
|
||
A few things are required to make this work: | ||
|
||
1) You should implement `Wireable` on all the data classes you'll be using with Livewire | ||
2) Each of these classes should also use the `WireableData` trait provided by this package | ||
3) That's it | ||
|
||
We can update our previous example to make it work with Livewire as such: | ||
|
||
```php | ||
class SongData extends Data implements Wireable | ||
{ | ||
use WireableData; | ||
|
||
public function __construct( | ||
public string $title, | ||
public string $artist, | ||
) { | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Concerns; | ||
|
||
use Spatie\LaravelData\Resolvers\DataFromSomethingResolver; | ||
|
||
trait WireableData | ||
{ | ||
public function toLivewire(): array | ||
{ | ||
return $this->toArray(); | ||
} | ||
|
||
public static function fromLivewire($value): static | ||
{ | ||
return app(DataFromSomethingResolver::class) | ||
->ignoreMagicalMethods('fromLivewire') | ||
->execute(static::class, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
ac30b6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rubenvanassche I'm really happy with Livewire support! I was now using a custom wireable trait, but this looks really good! :)