You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
@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.
@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.
Laravel API resources provide the ability to alias database fields in the JSON response, e.g.
And Laravel Orion provides Filters which expose the raw database fields, e.g.
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.Does this suggestion make sense? Or is there a better way?
The text was updated successfully, but these errors were encountered: