Skip to content

Commit ee3c9ce

Browse files
committed
livewire did not like an collection of arrays
1 parent 5885628 commit ee3c9ce

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

app/Livewire/Project/EditProject.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use App\Models\Legacy\ProjectPost;
99
use App\States\Project\ProjectState;
1010
use Cknow\Money\Money;
11-
use Illuminate\Support\Collection;
1211
use Illuminate\Support\Facades\Auth;
1312
use Illuminate\Support\Facades\Date;
1413
use Illuminate\Support\Facades\DB;
@@ -26,6 +25,7 @@ class EditProject extends Component
2625

2726
#[Url]
2827
public ?int $project_id = null;
28+
2929
#[Locked]
3030
public string $state_name;
3131

@@ -34,18 +34,28 @@ class EditProject extends Component
3434

3535
// Form data
3636
public string $name = '';
37+
3738
public string $responsible = '';
39+
3840
public string $org = '';
41+
3942
public string $org_mail = '';
43+
4044
public string $protokoll = '';
45+
4146
public string $beschreibung = '';
47+
4248
public string $recht = '';
49+
4350
public string $recht_additional = '';
51+
4452
public array $dateRange = [];
53+
4554
public int $hhp_id;
55+
4656
public int $version = 1;
4757

48-
public Collection $posts;
58+
public array $posts;
4959

5060
public array $attachments = [];
5161

@@ -55,7 +65,7 @@ public function mount(): void
5565

5666
if ($this->isNew) {
5767
Gate::authorize('create', Project::class);
58-
$project = new Project();
68+
$project = new Project;
5969
$this->populateData($project);
6070
$this->addEmptyPost();
6171
} else {
@@ -75,9 +85,12 @@ private function populateData(Project $project): void
7585
$this->beschreibung = $project->beschreibung ?? '';
7686
$this->recht = $project->recht ?? '';
7787
$this->recht_additional = $project->recht_additional ?? '';
78-
$this->dateRange = ['start' => $project->date_start, 'end' => $project->date_end];
79-
$this->version = $project->version;
80-
$this->hhp_id = LegacyBudgetPlan::findByDate($project->createdat)->id;
88+
$this->dateRange = [
89+
'start' => $project->date_start ?? null,
90+
'end' => $project->date_end ?? null,
91+
];
92+
$this->version = $project->version ?? 1;
93+
$this->hhp_id = LegacyBudgetPlan::findByDate($project->createdat)?->id;
8194
$this->state_name = $project->state->getValue();
8295
$this->posts = $project->posts->map(fn (ProjectPost $post) => [
8396
'id' => $post->id,
@@ -86,7 +99,7 @@ private function populateData(Project $project): void
8699
'einnahmen' => $post->einnahmen,
87100
'ausgaben' => $post->ausgaben,
88101
'titel_id' => $post->titel_id,
89-
]);
102+
])->all();
90103
$this->attachments = []; // FIXME: load Attachments
91104
}
92105

@@ -113,7 +126,7 @@ private function getValues(): array
113126
public function isPostDeletable(int $index): bool
114127
{
115128
return
116-
$this->posts->count() > 1 && (
129+
count($this->posts) > 1 && (
117130
(isset($this->posts[$index]['id']) && ExpenseReceiptPost::where('projekt_posten_id', $this->posts[$index]['id'])->doesntExist())
118131
|| ! isset($this->posts[$index]['id']));
119132
}
@@ -128,7 +141,7 @@ public function removePost(int $index): void
128141
}
129142
}
130143

131-
public function rules() : array
144+
public function rules(): array
132145
{
133146
return $this->getState()->rules();
134147
}
@@ -143,7 +156,6 @@ public function save()
143156
$filteredMeta = $filtered->except('posts')->toArray();
144157
$filteredPosts = $filtered->get('posts');
145158

146-
147159
try {
148160
DB::beginTransaction();
149161
if ($this->isNew) {
@@ -186,7 +198,7 @@ public function save()
186198
*/
187199
public function addEmptyPost(): void
188200
{
189-
$this->posts->add([
201+
$this->posts[] = ([
190202
'name' => '',
191203
'bemerkung' => '',
192204
'einnahmen' => Money::EUR(0),
@@ -200,15 +212,15 @@ public function addEmptyPost(): void
200212
*/
201213
public function getTotalIncome(): Money
202214
{
203-
return $this->posts->reduce(fn (?Money $carry, array $post) => $carry ? $carry->add($post['einnahmen']) : $post['einnahmen'], Money::EUR(0));
215+
return collect($this->posts)->reduce(fn (?Money $carry, array $post) => $carry ? $carry->add($post['einnahmen']) : $post['einnahmen'], Money::EUR(0));
204216
}
205217

206218
/**
207219
* Get the sum of all expense posts
208220
*/
209221
public function getTotalExpenses(): Money
210222
{
211-
return $this->posts->reduce(fn (?Money $carry, array $post) => $carry ? $carry->add($post['ausgaben']) : $post['ausgaben'], Money::EUR(0));
223+
return collect($this->posts)->reduce(fn (?Money $carry, array $post) => $carry ? $carry->add($post['ausgaben']) : $post['ausgaben'], Money::EUR(0));
212224
}
213225

214226
public function removeAttachment(int $index): void
@@ -276,13 +288,14 @@ public function render()
276288
}
277289

278290
#[Computed]
279-
public function getState(): ProjectState {
280-
return ProjectState::make($this->state_name, $this->getProject());
291+
public function getState(): ProjectState
292+
{
293+
return ProjectState::make($this->state_name, $this->getProject() ?? new Project);
281294
}
282295

283296
#[Computed]
284-
public function getProject(): Project {
285-
return Project::findOrFail($this->project_id);
297+
public function getProject(): ?Project
298+
{
299+
return Project::find($this->project_id);
286300
}
287-
288301
}

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@
110110
"rector": "@php vendor/bin/rector process",
111111
"rector-dry": "@php vendor/bin/rector process --dry-run",
112112
"lint": "@php vendor/bin/phpstan",
113-
"lint-fix": "@php vendor/bin/phpstan --fix",
114-
"pint": "@php vendor/bin/pint"
113+
"pint": "@php vendor/bin/pint",
114+
"fix" : [
115+
"@php vendor/bin/rector process",
116+
"@php vendor/bin/pint"
117+
]
115118
},
116119
"extra": {
117120
"laravel": {

0 commit comments

Comments
 (0)