Spatie has a package that somewhat handles this type of behaviour. It might not be exactly what you need; but source-diving will give you ideas about how you could implement your own
Dynamic API endpoint
Hello there,
I am looking into building an API endpoint which would return Model resource with given columns that are being sent as url request parameter.
Thing is that this should also work for model's relationships columns - meaning that my endpoint could look somewhat like this:
localhost/api/books?columns=id,title,description,author.first_name,author.last_name,distributor.id
This endpoint would return only columns id,title,description of the book model and then author's first and last name and distributor's id only
Does anybody know somewhat best practice which could be applied in this problem? I am looking for a way of building the controller index method and using laravel resources for this however I am confused with the way how to build the logic itself.
Thank you.
A when conditional for with
https://laravel.com/docs/8.x/queries#conditional-clauses
and this?
https://laravel.com/docs/8.x/eloquent-relationships#eager-loading-specific-columns
Please or to participate in this conversation.