From b1a4faaab19085c4e4a30bc8659c76ed77f2ba8a Mon Sep 17 00:00:00 2001 From: AureliusFarstad <74159788+AureliusFarstad@users.noreply.github.com> Date: Sun, 11 Jun 2023 21:33:20 +0300 Subject: [PATCH] Parent Child Table Progress --- .../Visitors/API/VisitorController.php | 20 ++-- app/Models/Visitors/ParentChild.php | 2 +- app/Models/Visitors/Visitor.php | 19 +++- .../js/components/visitors/VisitorDetails.vue | 1 - .../js/components/visitors/VisitorForm.vue | 93 ++++++++++++++++--- 5 files changed, 108 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/Visitors/API/VisitorController.php b/app/Http/Controllers/Visitors/API/VisitorController.php index 1d3724784..90185977b 100644 --- a/app/Http/Controllers/Visitors/API/VisitorController.php +++ b/app/Http/Controllers/Visitors/API/VisitorController.php @@ -106,6 +106,7 @@ public function createParentChild($parentId, $childId) $parent = Visitor::findOrFail($parentId); $child = Visitor::findOrFail($childId); + $parentChild = new ParentChild(); $parentChild->parent_id = $parent->id; $parentChild->child_id = $child->id; @@ -113,12 +114,8 @@ public function createParentChild($parentId, $childId) return; } - public function deleteParentChild($parentId, $childId) { - logger()->info('Deleting parent-child relationship', [ - 'parent_id' => $parentId, - 'child_id' => $childId, - ]); - + public function deleteParentChild($parentId, $childId) + { $parentChild = ParentChild::where('parent_id', $parentId) ->where('child_id', $childId) ->firstOrFail(); @@ -144,7 +141,7 @@ public function updateChildren(Visitor $visitor, StoreVisitor $request) $this->createParentChild($parentId, $childId); } - foreach ($createChildrenIds as $childId) { + foreach ($deleteChildrenIds as $childId) { $this->deleteParentChild($parentId, $childId); } return; @@ -156,12 +153,17 @@ public function updateParents(Visitor $visitor, StoreVisitor $request) $parentIds = collect($request->input('parents', [])) ->map(fn ($parent) => $parent['id']); - $existingParentIds = $visitor->children()->get() + $existingParentIds = $visitor->parents()->get() ->map(fn ($parent) => $parent->id); $childId = $visitor->id; $createParentIds = $parentIds->diff($existingParentIds); - $deleteParentIds = $existingParentIds->diff($parentIds); + logger()->info('Update', [ + 'Request parent ids' => $parentIds, + 'Existing parent ids' => $existingParentIds, + ]); + $deleteParentIds = $existingParentIds->diff($parentIds); //Broken line??? + // Error message: Call to a member function getKey() on int foreach ($createParentIds as $parentId) { $this->createParentChild($parentId, $childId); diff --git a/app/Models/Visitors/ParentChild.php b/app/Models/Visitors/ParentChild.php index 3d94d6a6d..d06b277ab 100644 --- a/app/Models/Visitors/ParentChild.php +++ b/app/Models/Visitors/ParentChild.php @@ -7,7 +7,7 @@ class ParentChild extends Model { - protected $table = 'parent_child'; + protected $table = 'parent_child'; //Visitor protected $fillable = [ 'parent_id', diff --git a/app/Models/Visitors/Visitor.php b/app/Models/Visitors/Visitor.php index d568523a6..9e48934b5 100644 --- a/app/Models/Visitors/Visitor.php +++ b/app/Models/Visitors/Visitor.php @@ -46,19 +46,23 @@ class Visitor extends Model 'parental_consent_given' => 'boolean', ]; - protected $appends = [ - 'parents', - 'children', - ]; + // protected $appends = [ + // 'parents', + // 'children', + // ]; public function checkins(): HasMany { return $this->hasMany(VisitorCheckin::class); } + // public function getParentsAttribute() + // { + // return $this->parents()->get(); + // } + public function parents() { - //wanted to use hasMany and parent() function in ParentChild model return $this->hasManyThrough( Visitor::class, ParentChild::class, @@ -69,6 +73,11 @@ public function parents() ); } + // public function getChildrenAttribute() + // { + // return $this->parents()->get(); + // } + public function children() { return $this->hasManyThrough( diff --git a/resources/js/components/visitors/VisitorDetails.vue b/resources/js/components/visitors/VisitorDetails.vue index b88557e16..60e76b59b 100644 --- a/resources/js/components/visitors/VisitorDetails.vue +++ b/resources/js/components/visitors/VisitorDetails.vue @@ -128,7 +128,6 @@ export default { }, }, data() { - console.log(this.value) return { visitor: this.value, isBusy: false, diff --git a/resources/js/components/visitors/VisitorForm.vue b/resources/js/components/visitors/VisitorForm.vue index 28e9ed2ed..49f7b5ccd 100644 --- a/resources/js/components/visitors/VisitorForm.vue +++ b/resources/js/components/visitors/VisitorForm.vue @@ -154,9 +154,9 @@ @@ -164,14 +164,50 @@
    -
  • +
  • {{ child.name }} - " + Remove
+ + + +