E_UNSAVED_MODEL_INSTANCE with relation

Hi,

I want display feed model in JSON with his articles (items).

Feed model:

items() {
    return this.belongsToMany('App/Models/Item', 'feed_items', 'feed_id', 'item_id')
}

Controller show:

async show({request, session, auth, response, params}) {

    const user = await auth.getUser()
    let feed = await Feed.query()
        .where('uuid', params.feedUuid)
        .with('items')
        .first()

    response.status(200).send(feed)
}

When I use with() method, I get this error:

RuntimeException: E_UNSAVED_MODEL_INSTANCE: Cannot process relation, since Feed model is not persisted to database or relational value is undefined

I don’t understand this error… How resolve it?

Thanks.

Can u share why you have defined feed_items as the 2nd param?

This error message emanates only from one specific situation: when the primary key you defined in the relationship related to the current model is wrong.

An other remark: Your relationship is being injected 4 parameters. That is wrong.

All relationships take 3 parameters at maximum: the related model, the primary key of the current model and, finally, the foreign key in the related model which refers to the current model.