I am wondering if it is possible to return a link to laravels with a route model binding?
Let's say a user has a model with friendships with other users, and I want to return both user information and relationships with a route or controller.
e.g. for route domain.tld/user/123
Route::model('user', 'User'); Route::get('/user/{user}', function(User $user) { return Response::json($user); });
this will return me the user information in order, but I also want a relationship, is there a simple / correct way to do this?
i know i can do it
Route::get('/user/{user}', function((User $user) { return Response::json(User::find($user['id'])->with('friends')->get()); });
or
Route::get('/user/{id}', function(($id) { return Response::json(User::find($id)->with('friends')->get()); });
but I suspect there might be a better way.
source share