API GET by Email
Hello all,
I'm trying to make a GET request through my API by using 'email' as the parameter instead of 'id'.
So with the default get by 'id' my code is as follows:
Route::get('user/{user}', 'GameController@show');
and in the Controller:
public function show(User $user)
{
return $user;
}
This works fine, but instead of using the implicit binding for 'id', how would I do this so my endpoint can be
user/{ emailhere } and return the user in the db with said email? Everything I've tried just seems to 404 so far.
public function show(User $user)
{
return $user;
}
This by default translates to
public function show( $id)
{
return User::where ('id', $id);
}
so, if you want to load the user by email, do this:
public function show( $user_email)
{
return User::where('user_email', $user_email)->firstOrFail();
}
Please or to participate in this conversation.