From d99c9bbf4a3bccd553aa14549f127e42653ec6ed Mon Sep 17 00:00:00 2001 From: AureliusFarstad <74159788+AureliusFarstad@users.noreply.github.com> Date: Tue, 27 Jun 2023 15:44:47 +0300 Subject: [PATCH] Code style and accessor type hints --- .../Visitors/API/VisitorController.php | 12 +++++----- app/Http/Resources/Visitors/Visitor.php | 4 ++-- app/Models/Visitors/ParentChild.php | 1 - app/Models/Visitors/Visitor.php | 24 ++++--------------- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Visitors/API/VisitorController.php b/app/Http/Controllers/Visitors/API/VisitorController.php index f161e1fc4..a684d607b 100644 --- a/app/Http/Controllers/Visitors/API/VisitorController.php +++ b/app/Http/Controllers/Visitors/API/VisitorController.php @@ -6,9 +6,9 @@ use App\Http\Controllers\Controller; use App\Http\Requests\Visitors\StoreVisitor; use App\Http\Resources\Visitors\Visitor as VisitorResource; +use App\Models\Visitors\ParentChild; use App\Models\Visitors\Visitor; use App\Models\Visitors\VisitorCheckin; -use App\Models\Visitors\ParentChild; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\JsonResponse; @@ -111,7 +111,7 @@ public function createParentChild($parentId, $childId) $parentChild->child_id = $child->id; $parentChild->save(); - return; + } public function deleteParentChild($parentId, $childId) @@ -121,7 +121,7 @@ public function deleteParentChild($parentId, $childId) ->firstOrFail(); $parentChild->delete(); - return; + } public function updateChildren(Visitor $visitor, StoreVisitor $request) @@ -145,7 +145,7 @@ public function updateChildren(Visitor $visitor, StoreVisitor $request) foreach ($deleteChildrenIds as $childId) { $this->deleteParentChild($parentId, $childId); } - return; + } public function updateParents(Visitor $visitor, StoreVisitor $request) @@ -169,7 +169,7 @@ public function updateParents(Visitor $visitor, StoreVisitor $request) foreach ($deleteParentIds as $parentId) { $this->deleteParentChild($parentId, $childId); } - return; + } public function update(Visitor $visitor, StoreVisitor $request): JsonResource @@ -180,7 +180,7 @@ public function update(Visitor $visitor, StoreVisitor $request): JsonResource $this->updateParents($visitor, $request); $visitor->fill($request->all()); - $visitor->save(); + $visitor->save(); return new VisitorResource($visitor); } diff --git a/app/Http/Resources/Visitors/Visitor.php b/app/Http/Resources/Visitors/Visitor.php index 7924a65e7..772c35132 100644 --- a/app/Http/Resources/Visitors/Visitor.php +++ b/app/Http/Resources/Visitors/Visitor.php @@ -30,12 +30,12 @@ public function toArray($request): array 'parental_consent_given' => $this->parental_consent_given, 'checked_in_today' => $this->whenLoaded('checkins', fn () => $this->checkins->contains(fn ($checkin) => $checkin->created_at->isToday())), - 'parents' => $this->whenLoaded('parents', + 'parents' => $this->whenLoaded('parents', fn () => $this->parents->map(fn ($parent) => [ 'id' => $parent->id, 'name' => $parent->name, ])), - 'children' => $this->whenLoaded('children', + 'children' => $this->whenLoaded('children', fn () => $this->children->map(fn ($child) => [ 'id' => $child->id, 'name' => $child->name, diff --git a/app/Models/Visitors/ParentChild.php b/app/Models/Visitors/ParentChild.php index d06b277ab..bf4a71492 100644 --- a/app/Models/Visitors/ParentChild.php +++ b/app/Models/Visitors/ParentChild.php @@ -2,7 +2,6 @@ namespace App\Models\Visitors; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class ParentChild extends Model diff --git a/app/Models/Visitors/Visitor.php b/app/Models/Visitors/Visitor.php index 9e48934b5..2209f2c6a 100644 --- a/app/Models/Visitors/Visitor.php +++ b/app/Models/Visitors/Visitor.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; class Visitor extends Model { @@ -46,26 +47,16 @@ class Visitor extends Model 'parental_consent_given' => 'boolean', ]; - // protected $appends = [ - // 'parents', - // 'children', - // ]; - public function checkins(): HasMany { return $this->hasMany(VisitorCheckin::class); } - // public function getParentsAttribute() - // { - // return $this->parents()->get(); - // } - - public function parents() + public function parents(): HasManyThrough { return $this->hasManyThrough( Visitor::class, - ParentChild::class, + ParentChild::class, 'child_id', 'id', 'id', @@ -73,16 +64,11 @@ public function parents() ); } - // public function getChildrenAttribute() - // { - // return $this->parents()->get(); - // } - - public function children() + public function children(): HasManyThrough { return $this->hasManyThrough( Visitor::class, - ParentChild::class, + ParentChild::class, 'parent_id', 'id', 'id',