Skip to content

Commit ca72fd2

Browse files
Update to Livewire 3
1 parent 3de65fc commit ca72fd2

5 files changed

Lines changed: 47 additions & 76 deletions

File tree

resources/install/resources/views/layouts/app.blade.php.stub

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
<!-- Scripts -->
1717
@vite(['resources/js/app.js'])
18-
@livewireStyles
1918
</head>
2019
<body>
2120
<div id="app">
@@ -79,13 +78,9 @@
7978
@yield('content')
8079
</main>
8180
</div>
82-
@livewireScripts
8381
<script type="module">
84-
const addModal = new bootstrap.Modal('#createDataModal');
85-
const editModal = new bootstrap.Modal('#updateDataModal');
8682
window.addEventListener('closeModal', () => {
87-
addModal.hide();
88-
editModal.hide();
83+
bootstrap.Modal.getInstance(document.getElementById('DataModal')).hide();
8984
})
9085
</script>
9186
</body>

src/Commands/LivewireGeneratorCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ protected function modelReplacements()
447447

448448
// Add quotes to the unwanted columns for fillable
449449
array_walk($filterColumns, function (&$value) {
450-
$value = "\n\t\t\t'" . $value . "' => \$this-> " . $value;
450+
$value = "\n\t\t\t\t'" . $value . "' => \$this-> " . $value;
451451
});
452452

453453
// CSV format
@@ -475,7 +475,7 @@ protected function modelReplacements()
475475

476476
// Add quotes to the unwanted columns for fillable */
477477
array_walk($filterColumns, function (&$value) {
478-
$value = "\n\t\t\t'" . $value . "' => \$this->faker->name,";
478+
$value = "\n\t\t\t'" . $value . "' => fake()->name(),";
479479
});
480480

481481
// CSV format

src/stubs/Livewire.stub

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3-
namespace App\Http\Livewire;
3+
namespace App\Livewire;
44

55
use Livewire\Component;
66
use Livewire\WithPagination;
77
use App\Models\{{modelName}};
8+
use Livewire\Attributes\Computed;
89

910
class {{modelName}}s extends Component
1011
{
@@ -13,57 +14,50 @@ class {{modelName}}s extends Component
1314
protected $paginationTheme = 'bootstrap';
1415
public $selected_id, $keyWord, {{updatefield}};
1516

16-
public function render()
17-
{
18-
$keyWord = '%'.$this->keyWord .'%';
19-
return view('livewire.{{modelNamePluralLowerCase}}.view', [
20-
'{{modelNamePluralLowerCase}}' => {{modelName}}::latest(){{search}}
21-
->paginate(10),
22-
]);
23-
}
17+
#[Computed]
18+
public function filtered{{modelName}}s()
19+
{
20+
$keyWord = '%' . $this->keyWord . '%';
21+
return {{modelName}}::latest()
22+
->where(function ($query) use ($keyWord) {
23+
$query{{search}};
24+
})
25+
->paginate(10);
26+
}
27+
28+
public function render()
29+
{
30+
return view('livewire.{{modelNamePluralLowerCase}}.view', [
31+
'{{modelNamePluralLowerCase}}' => $this->filtered{{modelName}}s,
32+
]);
33+
}
2434

2535
public function cancel()
2636
{
27-
$this->resetInput();
28-
}
29-
30-
private function resetInput()
31-
{ {{resetfields}}
37+
$this->reset();
3238
}
3339

34-
public function store()
40+
public function save()
3541
{
3642
$this->validate([{{rules}}
3743
]);
3844

39-
{{modelName}}::create([ {{addfields}}
40-
]);
41-
42-
$this->resetInput();
43-
$this->dispatchBrowserEvent('closeModal');
44-
session()->flash('message', '{{modelName}} Successfully created.');
45-
}
45+
Flight::updateOrCreate(
46+
['id' => $this->selected_id],
47+
[{{addfields}}
48+
]
49+
);
4650

47-
public function edit($id)
48-
{
49-
$record = {{modelName}}::findOrFail($id);
50-
$this->selected_id = $id; {{editfields}}
51+
$message = $this->selected_id ? '{{modelName}} Successfully updated.' : '{{modelName}} Successfully created.';
52+
$this->dispatch('closeModal');
53+
$this->reset();
54+
session()->flash('message', $message);
5155
}
5256

53-
public function update()
57+
public function edit($id)
5458
{
55-
$this->validate([{{rules}}
56-
]);
57-
58-
if ($this->selected_id) {
59-
$record = {{modelName}}::find($this->selected_id);
60-
$record->update([ {{addfields}}
61-
]);
62-
63-
$this->resetInput();
64-
$this->dispatchBrowserEvent('closeModal');
65-
session()->flash('message', '{{modelName}} Successfully updated.');
66-
}
59+
$this->selected_id = $id;
60+
$this->fill({{modelName}}::findOrFail($id)->toArray());
6761
}
6862

6963
public function destroy($id)

src/stubs/views/modals.stub

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
1-
<!-- Add Modal -->
2-
<div wire:ignore.self class="modal fade" id="createDataModal" data-bs-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="createDataModalLabel" aria-hidden="true">
1+
<!-- Modal -->
2+
<div wire:ignore.self class="modal fade" id="DataModal" data-bs-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="DataModalLabel" aria-hidden="true">
33
<div class="modal-dialog" role="document">
44
<div class="modal-content">
55
<div class="modal-header">
6-
<h5 class="modal-title" id="createDataModalLabel">Create New {{modelTitle}}</h5>
6+
<h5 class="modal-title" id="DataModalLabel">{{ $selected_id ? 'Update {{modelTitle}}' : 'Create {{modelTitle}}' }}</h5>
77
<button wire:click.prevent="cancel()" type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
88
</div>
99
<div class="modal-body">
10-
<form>{{form}}
10+
<form>
11+
@if ($selected_id)
12+
<input type="hidden" wire:model="selected_id">
13+
@endif{{form}}
1114
</form>
1215
</div>
1316
<div class="modal-footer">
1417
<button type="button" class="btn btn-secondary close-btn" data-bs-dismiss="modal">Close</button>
15-
<button type="button" wire:click.prevent="store()" class="btn btn-primary">Save</button>
18+
<button type="button" wire:click.prevent="save()" class="btn btn-primary">{{ $selected_id ? 'Update' : 'Create' }}</button>
1619
</div>
1720
</div>
1821
</div>
19-
</div>
20-
21-
<!-- Edit Modal -->
22-
<div wire:ignore.self class="modal fade" id="updateDataModal" data-bs-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="updateModalLabel" aria-hidden="true">
23-
<div class="modal-dialog" role="document">
24-
<div class="modal-content">
25-
<div class="modal-header">
26-
<h5 class="modal-title" id="updateModalLabel">Update {{modelTitle}}</h5>
27-
<button wire:click.prevent="cancel()" type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
28-
</div>
29-
<div class="modal-body">
30-
<form>
31-
<input type="hidden" wire:model="selected_id">{{form}}
32-
</form>
33-
</div>
34-
<div class="modal-footer">
35-
<button type="button" wire:click.prevent="cancel()" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
36-
<button type="button" wire:click.prevent="update()" class="btn btn-primary">Save</button>
37-
</div>
38-
</div>
39-
</div>
40-
</div>
22+
</div>

src/stubs/views/view.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div>
1616
<input wire:model.live='keyWord' type="text" class="form-control" name="search" id="search" placeholder="Search {{modelTitle}}s">
1717
</div>
18-
<div class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#createDataModal">
18+
<div class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#DataModal">
1919
<i class="bi-plus-lg"></i> Add {{modelTitle}}s
2020
</div>
2121
</div>
@@ -39,7 +39,7 @@
3939
Actions
4040
</a>
4141
<ul class="dropdown-menu">
42-
<li><a data-bs-toggle="modal" data-bs-target="#updateDataModal" class="dropdown-item" wire:click="edit({{$row->id}})"><i class="bi-pencil-square"></i> Edit </a></li>
42+
<li><a data-bs-toggle="modal" data-bs-target="#DataModal" class="dropdown-item" wire:click="edit({{$row->id}})"><i class="bi-pencil-square"></i> Edit </a></li>
4343
<li><a class="dropdown-item" onclick="confirm('Confirm Delete {{modelTitle}} id {{$row->id}}? \nDeleted {{modelTitle}}s cannot be recovered!')||event.stopImmediatePropagation()" wire:click="destroy({{$row->id}})"><i class="bi-trash3-fill"></i> Delete </a></li>
4444
</ul>
4545
</div>

0 commit comments

Comments
 (0)