Muaath Alhaddad
Muaath Alhaddad

Reputation: 361

Is there a way to get all the relations and fields of particular model in laravel?

I am trying to create a script that could help me generate a GraphQL schema with all fields and relations exist in the models.

if I could get that, php-lighthouse would help me for the rest of the work.

Upvotes: 0

Views: 416

Answers (1)

brausepulver
brausepulver

Reputation: 1

There is no way to get all relations of a particular Eloquent model without the user specifying them in some way, such as an attribute on the model that holds all relation names, or type-hinting those methods returning a relation. See laravel-eloquent-to-graphql for an example.

You can however get all fields of a model (that are saved in the database) by inspecting its table.

If you're looking for a GraphQL generator, there are Laravel Bakery and laravel-eloquent-to-graphql. Laravel Bakery generates a GraphQL schema on-the-fly, while laravel-eloquent-to-graphql generates a GraphQL schema statically.

There is also Lighthouse-Utils, which can generate queries and mutations for already-defined types.

Upvotes: 0

Related Questions