1
votes

I have the following tables:

users: id | name |

articles: id | active | global | version

and pivot table

articles_versions

user_id | articles_id | article_version

In model "user" I would like to have a method that checks if in pivot table "article_version" field is equal with field "version" in "article" table.

My current method in "user" model:

public function checkVersion()
{
    return $this->belongsToMany(
        Article::class,
        'articles_versions',
        'user_id',
        'article_id'
    )
        ->where('version', 'articles_versions.article_version');
        //OR
        //->wherePivot('version', 'articles_versions.article_version');
}

How I can pass to "where" or "wherePivot" value to check from the related table?

Is it possible to do it?

Thank you

1

1 Answers

0
votes

Use whereColumn():

public function checkVersion()
{
    return $this->belongsToMany(
        Article::class,
        'articles_versions',
        'user_id',
        'article_id'
    )->whereColumn('version', 'articles_versions.article_version');
}