Skip to content

Commit

Permalink
Code style and accessor type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliusFarstad committed Jun 27, 2023
1 parent 64018ee commit d99c9bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
12 changes: 6 additions & 6 deletions app/Http/Controllers/Visitors/API/VisitorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function createParentChild($parentId, $childId)
$parentChild->child_id = $child->id;

$parentChild->save();
return;

}

public function deleteParentChild($parentId, $childId)
Expand All @@ -121,7 +121,7 @@ public function deleteParentChild($parentId, $childId)
->firstOrFail();

$parentChild->delete();
return;

}

public function updateChildren(Visitor $visitor, StoreVisitor $request)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Resources/Visitors/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion app/Models/Visitors/ParentChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Models\Visitors;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ParentChild extends Model
Expand Down
24 changes: 5 additions & 19 deletions app/Models/Visitors/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -46,43 +47,28 @@ 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',
'parent_id'
);
}

// 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',
Expand Down

0 comments on commit d99c9bb

Please sign in to comment.