Skip to content

Commit

Permalink
added user companies to api
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Sep 19, 2017
1 parent 9281e2b commit 1dd984b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/Api/Auth/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Users extends ApiController
*/
public function index()
{
$users = User::with(['roles', 'permissions'])->collect();
$users = User::with(['companies', 'roles', 'permissions'])->collect();

return $this->response->paginator($users, new Transformer());
}
Expand All @@ -34,9 +34,9 @@ public function show($id)
{
// Check if we're querying by id or email
if (is_numeric($id)) {
$user = User::with(['roles', 'permissions'])->findOrFail($id);
$user = User::with(['companies', 'roles', 'permissions'])->findOrFail($id);
} else {
$user = User::with(['roles', 'permissions'])->where('email', $id)->first();
$user = User::with(['companies', 'roles', 'permissions'])->where('email', $id)->first();
}

return $this->response->item($user, new Transformer());
Expand Down
12 changes: 11 additions & 1 deletion app/Http/Transformers/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Transformers\Auth;

use App\Http\Transformers\Company\Company;
use App\Models\Auth\User as Model;
use League\Fractal\TransformerAbstract;

Expand All @@ -10,7 +11,7 @@ class User extends TransformerAbstract
/**
* @var array
*/
protected $defaultIncludes = ['roles'];
protected $defaultIncludes = ['companies', 'roles'];

/**
* @param Model $model
Expand All @@ -27,6 +28,15 @@ public function transform(Model $model)
];
}

/**
* @param Model $model
* @return \League\Fractal\Resource\Collection
*/
public function includeCompanies(Model $model)
{
return $this->collection($model->companies, new Company());
}

/**
* @param Model $model
* @return \League\Fractal\Resource\Collection
Expand Down

0 comments on commit 1dd984b

Please sign in to comment.