0

Laravel's Lighthouse has directives to perform data-modifying CRUD operations directly on the database: @create, @delete, and @update.

A simple CRUD's create can be implemented like this:

type Mutation {
  createPost(title: String!): Post @create
}

This works, but it creates records without an owner. My question is: How to assign an user_id to a newly created record?

I mean, without having to use a resolver.

I could add an user_id parameter, like this:

type Mutation {
  createPost(user_id: ID, title: String!): Post @create
}

But that would cause a huge security issue - anyone could add or change records of other users.

Daniel Loureiro
  • 4,595
  • 34
  • 48

2 Answers2

1

You can use @inject directive, like @inject(context: "user.id", name: "user_id").

Enzo Notario
  • 639
  • 4
  • 9
1

Alternatively, you can use an observer on your model and save the user_id there with auth()->id();

Gregory
  • 1,148
  • 1
  • 9
  • 24