From 771e3b01c530d9563f5eb0432f4aaf17fb7228e9 Mon Sep 17 00:00:00 2001 From: Brian Davis Date: Wed, 4 Jan 2023 07:59:25 -0500 Subject: [PATCH] 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? --- app/Models/Forms/Form.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Models/Forms/Form.php b/app/Models/Forms/Form.php index 2d8195b..e0253a5 100644 --- a/app/Models/Forms/Form.php +++ b/app/Models/Forms/Form.php @@ -137,8 +137,13 @@ class Form extends Model 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)")); + } elseif(env('DB_CONNECTION') == 'mysql') { + return $this->views()->count() + + $this->statistics()->sum(DB::raw("json_extract(data, '$.views')")); // code to + } } public function setDescriptionAttribute($value)