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

Commit

Permalink
Merge pull request #209 from yondifon/master
Browse files Browse the repository at this point in the history
Edit Children
  • Loading branch information
curtisdelicata authored Jul 14, 2021
2 parents 64785b9 + f5cfb01 commit 8215daa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
86 changes: 58 additions & 28 deletions app/Http/Controllers/FamilyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Family;
use App\Models\Person;
use DB;
use Illuminate\Http\Request;

class FamilyController extends Controller
Expand All @@ -22,7 +21,7 @@ public function index(Request $request)
$columnsToSearch = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id', 'chan', 'nchi', 'rin'];
$search_term = json_decode($request->searchTerm)->searchTerm;
if (! empty($search_term)) {
$searchQuery = '%'.$search_term.'%';
$searchQuery = '%' . $search_term . '%';
foreach ($columnsToSearch as $column) {
$query->orWhere($column, 'LIKE', $searchQuery);
}
Expand All @@ -34,7 +33,7 @@ public function index(Request $request)

foreach ($filters as $key => $value) {
if (! empty($value)) {
$query->orWhere($key, 'like', '%'.$value.'%');
$query->orWhere($key, 'like', '%' . $value . '%');
}
}
}
Expand Down Expand Up @@ -63,9 +62,11 @@ public function create()
{
$male = Person::where('sex', 'M')->get();
$female = Person::where('sex', 'F')->get();
$types = DB::table('types')->get();
$persons = Person::all();

return response()->json(['male' => $male, 'female' => $female, 'types' => $types]);
$types = \DB::table('types')->get();

return response()->json(['male' => $male, 'female' => $female, 'types' => $types, 'persons' => $persons]);
}

/**
Expand All @@ -78,25 +79,29 @@ public function store(Request $request)
{
$request->validate([
'description' => 'required',
'is_active' => 'required',
'husband_id' => 'required',
'wife_id' => 'required',
'type_id' => 'required',
'chan' => 'required',
'nchi' => 'required',
'rin' => 'required',
'is_active' => 'required',
'husband_id' => 'required',
'wife_id' => 'required',
'type_id' => 'required',
'chan' => 'required',
'nchi' => 'required',
'rin' => 'required',
]);

return Family::create([
$family = Family::create([
'description' => $request->description,
'is_active' => $request->is_active,
'husband_id' => $request->husband_id,
'wife_id' => $request->wife_id,
'type_id' => $request->type_id,
'chan' => $request->chan,
'nchi' => $request->nchi,
'rin' => $request->rin,
'is_active' => $request->is_active,
'husband_id' => $request->husband_id,
'wife_id' => $request->wife_id,
'type_id' => $request->type_id,
'chan' => $request->chan,
'nchi' => $request->nchi,
'rin' => $request->rin,
]);

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

return $family;
}

/**
Expand All @@ -107,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 All @@ -132,13 +141,13 @@ public function update(Request $request, $id)
{
$request->validate([
'description' => 'required',
'is_active' => 'required',
'husband_id' => 'required',
'wife_id' => 'required',
'type_id' => 'required',
'chan' => 'required',
'nchi' => 'required',
'rin' => 'required',
'is_active' => 'required',
'husband_id' => 'required',
'wife_id' => 'required',
'type_id' => 'required',
'chan' => 'required',
'nchi' => 'required',
'rin' => 'required',
]);

$family = Family::find($id);
Expand All @@ -152,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 @@ -172,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 8215daa

Please sign in to comment.