Skip to content

Commit

Permalink
join person table with category and group
Browse files Browse the repository at this point in the history
  • Loading branch information
martiendt committed Jun 13, 2018
1 parent c90aa4f commit e89fc76
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions app/Http/Controllers/Api/Master/PersonController.php
Expand Up @@ -23,7 +23,17 @@ public function index(Request $request)
{
$limit = $request->input('limit') ?? 0;

return new PersonCollection(Person::paginate($limit));
$persons = Person::join('person_categories', 'person_categories.id', '=', 'persons.person_category_id')
->join('person_groups', 'person_groups.id', '=', 'persons.person_group_id')
->select('persons.*', 'person_categories.name as category_name', 'person_groups.name as group_name');

if ($request->input('category')) {
$persons = $persons->where('person_categories.name', $request->input('category'));
}

$persons = $persons->paginate($limit);

return new PersonCollection($persons);
}

/**
Expand Down Expand Up @@ -58,7 +68,13 @@ public function store(StorePersonRequest $request)
*/
public function show($id)
{
return new PersonResource(Person::findOrFail($id));
$person = Person::join('person_categories', 'person_categories.id', '=', 'persons.person_category_id')
->join('person_groups', 'person_groups.id', '=', 'persons.person_group_id')
->where('persons.id', $id)
->select('persons.*', 'person_categories.name as category_name', 'person_groups.name as group_name')
->first();

return new PersonResource($person);
}

/**
Expand Down

0 comments on commit e89fc76

Please sign in to comment.