0

Is there a way to get resources (example: Posts) via the authenticated user? Like this:

Auth::user()->posts()

Schema:

type Query @guard(with: ["api"]) {
    me_posts: [Post!]! @auth
}
degebine
  • 303
  • 2
  • 11
  • Should be `Auth::user()->posts` or if there is a chance user is not authenticated then better to use with check: `optional(Auth::user())->posts` since property gives you a collection while method gives you a query builder. Pay attention on `->posts` vs. `->posts()`. – Tpojka Jan 22 '21 at 10:53
  • You can also use a scope, so you won't need a custom field resolver. – Enzo Notario Jan 22 '21 at 12:48
  • I would rather use `{ me { posts {...} } }`. And in Scheme: `{me: User @auth}`, and `type User { posts: [Post!]! @hasMany }` – lorado Jan 23 '21 at 13:24
  • you can custom resolver and `return Auth::user()->posts()`. you can try the `@whereAuth` using the relation too. – francisco Sep 06 '21 at 14:08

0 Answers0