Reputation: 61
I wonder if this is possible, imagine that I have this reply on a laravel
api when I call all the users, I wonder if it's possible just calling one of them and showing the information related to that user like public/api/users/1
I cannot find the words to search a reply for that, so sorry if its a repost.
{
"id": 1,
"nome": "a",
"BI": 123,
"morada": "1245"
},
{
"id": 2,
"nome": "b",
"BI": 123456,
"morada": "1p231p2oi3"
}
Calling just through public/api/users/1
gives me a blank page, that would supposed to be the id
of the user right?
Upvotes: 0
Views: 80
Reputation: 4229
As a straightforward implementation of that, you could add to routes.php file:
Route::get("api/users/{id}", function($id) {
return App\User::find($id);
});
As of Laravel 5.2 framework also supports model binding:
Route::get('api/users/{user}', function (App\User $user) {
return $user;
});
EDIT:
Outputing only certain attributes:
Route::get('api/users/{user}', function (App\User $user) {
return response()->json([
"id" => (int) $user->id,
"name" => $user->name
]);
});
Upvotes: 1
Reputation: 30565
I suggest using something like the below.
Route::get('api/users/{user}', function ($id) {
$user = App\User::first($id);
$content = [
'id' => $user->id,
'nome' => $user->nome,
'BI' => $user->BI,
'morada' => $user->morada
];
return response($content, 200)
->header('Content-Type', 'application/json');
});
Upvotes: 1