Column 'is_published' in where clause is ambiguous

Basically, I have 2 models with the same is_published boolean.
first model belongs to Many 2nd model and both has a scope isPublished.

now, the relationships between them is like this:

public $hasManyThrough = [
        'lessons_published' => [
            Lesson::class,
            'through' => Module::class,
            'scope' => 'published'
        ],
    ];

when I try to access the relation lessons_published from an instance of the 1st model Course, I have this sql error

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'is_published' in where clause is ambiguous (SQL: 

anyone encoutnered this before?

Hi @chris,

I know it’s possible to specify the table in your scope, like this:

public function scopePublished($query, $filter) {
   $query->whereIn('your_table.is_published', $filter);
}

Would this work for you ?

2 Likes

ah super! it works great in the scope indeed.

also works fine here

->withCount('lessons_published')
                 ->withCount(['lessons' => function ($query) {
                     $query->where('table_lessons.is_published', true);
                 }])

thanks a lot