Adil Amanat
Adil Amanat

Reputation: 70

lighthouse php use eloquent model accessors and mutators

I'm using lighthouse for graphql on top of my laravel application. I know how to define types. But how can I use the model accessor in the type definition of schema? For example, i have type use like this

type User {
   id: ID!
   name: String
}

But I don't have a name column in the database table but I have a name accessor in my eloquent model which I want to use in this case instead of weird column name. Is there any way I can use the model accessor?

Upvotes: 0

Views: 1274

Answers (2)

isma0611
isma0611

Reputation: 63

Your code should work fine if you are using laravel accessor get{Attribute}Attribute, you can also use @method directive:

type User {
   #...
   name: String @method(name: "getName")
}

Checkout: https://lighthouse-php.com/master/api-reference/directives.html#method & https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

Upvotes: 1

lorado
lorado

Reputation: 336

Do you mean methods like getFirstNameAttribute? Simply define a field in snake case in your scheme, for my example it would be first_name: String

type User {
   #...
   first_name: String
}

Upvotes: 2

Related Questions