Skip to content


Object type scalar with Laravel Lighthouse

Laravel, GraphQL1 min read

By default, GraphQL doesn’t support object type, fortunately we call customize and create a scalar type by own self. In laravel lighthouse, you can use creating scalar to suit what you need, here are steps:

First of all, Creating a scalar class.


You would get the methods and default ways.

You can do the json_encode or json_decode in here, but we can use the eloquent of laravel feature: casting in model.

By the way, I suggest your column type in MySQL should be json type, it was a new feature since MySQL 5.6.

For example, we have a model called Product, and the one of columns is description, it should be put several data in json type, so it might look like:

Next, we need to set the casting in model, it's a important thing to do, it tells laravel, hey, you need to cast the column of specification.

Accomplish! pretty easy right? you can use playground to see it correctly: result

© 2020 by Yish. All rights reserved.