Created
May 13, 2021 02:59
-
-
Save coreymcmahon/b0761624a9fa87802f30bd0e17b46df1 to your computer and use it in GitHub Desktop.
Interview example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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