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
Hi, i'm looking into using package in my project and have a question about appending attributes.
I'd like to add conditional attributes to response based on query result.
Let's say i have Post model with comments relation
class Post extends Model {
publicfunctioncomments()
{
return$this->morphToMany(Comment::class, 'commentable');
}
publicfunctionscopeWithCommentsStatistics(Builder$query)
{
$query->/** Some complex query, which counts additional columns **/
}
publicfunctiongetCommentsStatisticsAttribute()
{
return [
// additional columns from withCommentsStatistics() scope
];
}
}
I can get posts list from PostController like this:
With following request /post?filter[comments_statistics]=1&append=comments_statistics
So the question is there any way to get my comments_statistics attribute with only /post?append=comments_statistics?
It could be done applying withCommentsStatistics() scope to all queries, like global scope, but i don't feel like having complex part of query in all queries that works with Post model.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, i'm looking into using package in my project and have a question about appending attributes.
I'd like to add conditional attributes to response based on query result.
Let's say i have
Post
model withcomments
relationI can get posts list from
PostController
like this:With following request
/post?filter[comments_statistics]=1&append=comments_statistics
So the question is there any way to get my comments_statistics attribute with only
/post?append=comments_statistics
?It could be done applying withCommentsStatistics() scope to all queries, like global scope, but i don't feel like having complex part of query in all queries that works with
Post
model.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions