Skip to content

Commit 786a54b

Browse files
Tofandelifox
authored andcommitted
Fix published scope
1 parent 76df61a commit 786a54b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/Models/Model.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ protected function isTranslationModel(): bool
3333
return Str::endsWith(get_class($this), 'Translation');
3434
}
3535

36-
public function scopePublished($query): Builder
36+
public function scopePublished(Builder $query): Builder
3737
{
38-
return $query->where("{$this->getTable()}.published", true);
38+
if ($this->isFillable('published')) {
39+
return $query->where($query->qualifyColumn('published'), true);
40+
}
41+
return $query;
3942
}
4043

41-
public function scopeAccessible($query): Builder
44+
public function scopeAccessible(Builder $query): Builder
4245
{
4346
if (! TwillPermissions::enabled()) {
4447
return $query;
@@ -75,11 +78,11 @@ public function scopeAccessible($query): Builder
7578
return $query;
7679
}
7780

78-
public function scopePublishedInListings($query): Builder
81+
public function scopePublishedInListings(Builder $query): Builder
7982
{
8083
// @todo: Remove? Seems unused.
8184
if ($this->isFillable('public')) {
82-
$query->where("{$this->getTable()}.public", true);
85+
$query->where($query->qualifyColumn('public'), true);
8386
}
8487

8588
return $query->published()->visible();
@@ -88,21 +91,21 @@ public function scopePublishedInListings($query): Builder
8891
/**
8992
* @todo: Document
9093
*/
91-
public function scopeVisible($query): Builder
94+
public function scopeVisible(Builder $query): Builder
9295
{
9396
if ($this->isFillable('publish_start_date')) {
9497
$query->where(function ($query) {
95-
$query->whereNull("{$this->getTable()}.publish_start_date")->orWhere(
96-
"{$this->getTable()}.publish_start_date",
98+
$query->whereNull($query->qualifyColumn('publish_start_date'))->orWhere(
99+
$query->qualifyColumn('publish_start_date'),
97100
'<=',
98101
Carbon::now()
99102
);
100103
});
101104

102105
if ($this->isFillable('publish_end_date')) {
103106
$query->where(function ($query) {
104-
$query->whereNull("{$this->getTable()}.publish_end_date")->orWhere(
105-
"{$this->getTable()}.publish_end_date",
107+
$query->whereNull($query->qualifyColumn('publish_end_date'))->orWhere(
108+
$query->qualifyColumn('publish_end_date'),
106109
'>=',
107110
Carbon::now()
108111
);
@@ -118,12 +121,15 @@ public function setPublishStartDateAttribute($value): void
118121
$this->attributes['publish_start_date'] = $value ?? Carbon::now();
119122
}
120123

121-
public function scopeDraft($query): Builder
124+
public function scopeDraft(Builder $query): Builder
122125
{
123-
return $query->where("{$this->getTable()}.published", false);
126+
if ($this->isFillable('published')) {
127+
return $query->where($query->qualifyColumn('published'), false);
128+
}
129+
return $query;
124130
}
125131

126-
public function scopeOnlyTrashed($query): Builder
132+
public function scopeOnlyTrashed(Builder $query): Builder
127133
{
128134
return $query->onlyTrashed();
129135
}

0 commit comments

Comments
 (0)