Form views count compatible with mysql (#58)

getViewsCountAttribute is using a raw SQL statement that is specific to pgsql, it fails on MySQL/MariaDB.  Added a conditional to use mysql specific code to access data in the json array.  Crude, but no more so than the original?
This commit is contained in:
Brian Davis 2023-01-04 07:59:25 -05:00 committed by GitHub
parent 9f1f968430
commit 771e3b01c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -137,8 +137,13 @@ class Form extends Model
public function getViewsCountAttribute() public function getViewsCountAttribute()
{ {
return $this->views()->count() + if(env('DB_CONNECTION') == 'pgsql') {
return $this->views()->count() +
$this->statistics()->sum(DB::raw("cast(data->>'views' as integer)")); $this->statistics()->sum(DB::raw("cast(data->>'views' as integer)"));
} elseif(env('DB_CONNECTION') == 'mysql') {
return $this->views()->count() +
$this->statistics()->sum(DB::raw("json_extract(data, '$.views')")); // code to
}
} }
public function setDescriptionAttribute($value) public function setDescriptionAttribute($value)