Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Select Children
Browse files Browse the repository at this point in the history
  • Loading branch information
yondifon committed Jul 14, 2021
1 parent 1866d72 commit f5cfb01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
31 changes: 28 additions & 3 deletions app/Http/Controllers/FamilyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function create()
$female = Person::where('sex', 'F')->get();
$persons = Person::all();

$types = DBtable('types')->get();
$types = \DB::table('types')->get();

return response()->json(['male' => $male, 'female' => $female, 'types' => $types, 'persons' => $persons]);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ public function store(Request $request)
'rin' => $request->rin,
]);

$family->children($request->chlildren);
$this->syncChildren($family, (array) $request->children);

return $family;
}
Expand All @@ -112,7 +112,11 @@ public function store(Request $request)
*/
public function show($id)
{
return Family::find($id);
$family = Family::find($id);

$family->load('children');

return $family;
}

/**
Expand Down Expand Up @@ -157,6 +161,8 @@ public function update(Request $request, $id)
$family->rin = $request->rin;
$family->save();

$this->syncChildren($family, (array) $request->children);

return $family;
}

Expand All @@ -177,4 +183,23 @@ public function destroy($id)

return 'false';
}

protected function syncChildren(Family $family, array $children)
{
$old_children = $family->children;

foreach ($old_children as $ch) {
if (! \in_array($ch->id, $children)) {
$ch->child_in_family_id = null;
$ch->save();
}
}

$children = Person::find($children);

foreach ($children as $child) {
$child->child_in_family_id = $family->id;
$child->save();
}
}
}
1 change: 1 addition & 0 deletions app/Models/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
class Type extends Model
{
use HasFactory;

protected $fillable = ['name', 'description', 'is_active'];
}

0 comments on commit f5cfb01

Please sign in to comment.