diff --git a/app/Http/Controllers/FamilyController.php b/app/Http/Controllers/FamilyController.php index f5acda9..3c5326a 100755 --- a/app/Http/Controllers/FamilyController.php +++ b/app/Http/Controllers/FamilyController.php @@ -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]); } @@ -99,7 +99,7 @@ public function store(Request $request) 'rin' => $request->rin, ]); - $family->children($request->chlildren); + $this->syncChildren($family, (array) $request->children); return $family; } @@ -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; } /** @@ -157,6 +161,8 @@ public function update(Request $request, $id) $family->rin = $request->rin; $family->save(); + $this->syncChildren($family, (array) $request->children); + return $family; } @@ -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(); + } + } } diff --git a/app/Models/Type.php b/app/Models/Type.php index f4185ea..27f7e00 100755 --- a/app/Models/Type.php +++ b/app/Models/Type.php @@ -8,5 +8,6 @@ class Type extends Model { use HasFactory; + protected $fillable = ['name', 'description', 'is_active']; }