Relationship having where condition

How can I achieve creating a relationship with a filter or where condition?

    public function available_videos() {
        return $this->hasMany('Video')->where('available','=', 1);
    }

Link to Laravel counterpart

Same as Laravel

In a controller/service:

const movies = 
   await Movie.query()
        .with("videos", builder => { builder.where("status_cd", "available")})
        .whereHas("videos", builder => { builder.where("status_cd", "available")}, "=", 1)
        .fetch();
        

In a model: similar to above.

https://adonisjs.com/docs/4.1/relationships#_wherehas

1 Like