Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to alias filter column names #137

Open
anthonymf opened this issue Dec 12, 2021 · 4 comments
Open

Ability to alias filter column names #137

anthonymf opened this issue Dec 12, 2021 · 4 comments
Labels
question Further information is requested

Comments

@anthonymf
Copy link

Laravel API resources provide the ability to alias database fields in the JSON response, e.g.

public function toArray($request):
{
    return [
        'currency_id' => $this->currency_id,
        'currency_name' => $this->curr_name, // currency_name is nicer than curr_name
    ];
}

And Laravel Orion provides Filters which expose the raw database fields, e.g.

public function filterableBy() : array
{
    return ['curr_name'];
}

But this means that the frontend developer has a "nice" name in the JSON response but must use a "raw" database field name in the filter.

So it would be helpful to the frontend developer if it was possible to expose filterableBy attributes via an alias, e.g.

public function filterableBy() : array
{
    return [
        'currency_name' => 'curr_name',  // perhaps this could be a way to specify a filter alias?
    ];
}

Does this suggestion make sense? Or is there a better way?

@alexzarbn
Copy link
Member

Hi @anthonymf,

Why not just rename the column in db? 🤔 Column names should be easy to understand on both frontend and backend.

@alexzarbn alexzarbn added the question Further information is requested label Dec 19, 2021
@anthonymf
Copy link
Author

anthonymf commented Dec 19, 2021

@alexzarbn Renaming the db column would not suit use cases where the DB is tightly coupled to other systems or other existing code in the Laravel application.

But I think the principle of Laravel API Resources is that they allow fields in JSON responses to be aliased in the response, rather than having to do this in the database. This neatly decouples the API responses from the db structure.
I’m suggesting that an ability to alias fields in ‘filterableBy‘ would be useful in the same way.

@alexzarbn
Copy link
Member

@anthonymf Okay, that makes sense. I think introducing a method aliases that lists such fields would work. It can be used by QueryBuilder for filtering and sorting, and by controllers to map incoming request attributes onto model attributes.

@anthonymf
Copy link
Author

@alexzarbn Yes, that sounds like a good plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants