User-Based `computed` attributes of Models?

How to get a computed attribute like is_liked of a Post model based on requested user via JWT token?

  1. use addHook in static boot() of Post Model
  2. how to access current auth.user ?

Do u have the post id?

There is a table called user_posts with user_id and post_id fields save the user likes information. so I have to query this table to check if I liked this Post.

And then, I need to retrieve is_liked attribute in both Post.fetch() and Post.first() scenarios.

Can u share the relationships on the Lucid end? Which models and what all relationships do they have

class Post {
  user() {
    return this.belongsTo('App/Models/User', 'user_id', '_id').select(User.listFields)
    // just like a getter, but the raw getter in lucid is only support sync operations
    // and I need to access **current request user**, that's the problem.
    // return await user.actions().where({
    //   actionable_type: 'Post',
    //   actionable_id: this._id,
    //  }).count() > 0
class User {
  actions() {
    return this.hasMany('App/Models/Action', '_id', 'user_id')
class Action {
  //_id, actionable_id, actionable_type, name, user_id
  //1, 2, 'Post', 'like', 3      //which means: User 3 liked Post 2
  //2, 2, 'Post', 'collect', 3   //User 3 collected Post 2
  //3, 3, 'Post', 'like', 3      //User 3 liked Post 3

anyone can help me? I have no idea for that

Same problem here… +1

You are trying to polymorphic relationship structure here, which AdonisJs doesn’t support. What you need is Post belongsToMany User relationship, and store is_liked in pivot table

That will not resolve the problem. Post.user will return a collection of users that favorited that post. We want Post.is_liked to return true if auth.user favorited that post or false if not.

In other words, is there any way I can use auth.user in a Model?

1 Like

There is an example in docs, where you can query the pivot table and filter data.
I haven’t tried it, but sounds legit to add to that table your custom fields and query against them.
Btw, you can create a new global for View, kinda is_liked where you can check all the stuff, again, didn’t try it, a pure theoretical proposal.

That’s right, e.g:
I can use \Auth::user() to fetch current logged user every where in Laravel application.

Node and Php are not same, so it’s more Apple vs Oranges comparison

Hello @wxs77577 @utwo

It’s probable been ages but did any of you guys figure a way to work around this?

How did you guys finally get the is_liked computed property for the Post model? It’s pretty easy to get this working in Laravel with\Auth::user() but since Adonis doesn’t provide global auth due to the nature of session(JWT) how did you guys finally get this working in your projects?

I’m currently building a social network and I would love to add the ‘likedByCurrentUser’ computed into the returned posts.toJSON() .

You guy’s solution will be highly appreciated :pray:

Thanks so much in advance.

Sorry, I didn’t find any solution :frowning: