Skip to content

Instantly share code, notes, and snippets.

@coreymcmahon
Created May 13, 2021 02:59
Show Gist options
  • Save coreymcmahon/b0761624a9fa87802f30bd0e17b46df1 to your computer and use it in GitHub Desktop.
Save coreymcmahon/b0761624a9fa87802f30bd0e17b46df1 to your computer and use it in GitHub Desktop.
Interview example
<?php
/**
* In routes/api.php
*/
$router->get('api/v2/users', 'UsersController@index');
/**
* In app/Users.php
*/
class User extends Model
{
protected $fillable = ['name', 'password'];
public function addresses()
{
return $this->hasMany(\App\Address::class);
}
}
/**
* In app/Controllers/UsersController.php
*/
class UsersController
{
public function index()
{
$users = User::query()->get();
$data = [];
foreach ($users as $user) {
$data[] = [
'id' => $user->id,
'name' => $user->name,
'addresses' => $user->addresses->toArray(),
];
}
return response()->json($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment