I'm trying to create laravel api with the NEXT JS app. In my app, users can support the horse. On the listing page, authentication can see horses, and this user can see his supported horse. So, I made this code.
@shaungbhone Please have a read over the Eloquent relationships chapter of the Laravel docs. That code is woefully inefficient and will give you N+1 problems.
You’re also not actually returning anything from your index action.
You don‘t need to use the auth helper to get the authenticated user. You can get them from the request instance using $request->user() (so long as the route is behind auth middleware).
@shaungbhone I don’t really understand what that has to do with anything? A client gives you problems, and then you write the code to solve those problems.
You don’t need a client to tell you when and when you can’t use relations in Eloquent models. Otherwise that client may as well write the code themselves in they know exactly what they want.