From d6ef9ddc3703f0bd4080cf807495bf2d0ccdd1cc Mon Sep 17 00:00:00 2001 From: FreeScout Date: Sat, 17 Dec 2022 22:20:43 -0800 Subject: [PATCH] Add meta column to conversations table --- app/Conversation.php | 84 ++++++++----------- ...add_meta_column_to_conversations_table.php | 33 ++++++++ 2 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 database/migrations/2022_12_17_010101_add_meta_column_to_conversations_table.php diff --git a/app/Conversation.php b/app/Conversation.php index f1eeb728..9c88c9f4 100644 --- a/app/Conversation.php +++ b/app/Conversation.php @@ -208,6 +208,13 @@ class Conversation extends Model */ protected $guarded = ['id', 'folder_id']; + /** + * Convert to array. + */ + protected $casts = [ + 'meta' => 'array', + ]; + /** * Default values. */ @@ -499,6 +506,11 @@ class Conversation extends Model return $this->status == self::STATUS_SPAM; } + public function isClosed() + { + return $this->status == self::STATUS_CLOSED; + } + /** * Get status name. * @@ -2235,55 +2247,29 @@ class Conversation extends Model } } - // /** - // * Get conversation meta data as array. - // */ - // public function getMetas() - // { - // return \Helper::jsonToArray($this->meta); - // } + /** + * Get meta value. + */ + public function getMeta($key, $default = null) + { + if (isset($this->meta[$key])) { + return $this->meta[$key]; + } else { + return $default; + } + } - // /** - // * Set conversation meta value. - // */ - // public function setMetas($data) - // { - // $this->meta = json_encode($data); - // } + /** + * Set meta value. + */ + public function setMeta($key, $value, $save = false) + { + $meta = $this->meta; + $meta[$key] = $value; + $this->meta = $meta; - // /** - // * Get conversation meta value. - // */ - // public function getMeta($key, $default = null) - // { - // $metas = $this->getMetas(); - // if (isset($metas[$key])) { - // return $metas[$key]; - // } else { - // return $default; - // } - // } - - // /** - // * Set conversation meta value. - // */ - // public function setMeta($key, $value) - // { - // $metas = $this->getMetas(); - // $metas[$key] = $value; - // $this->setMetas($metas); - // } - - // /** - // * Create new conversation. - // */ - // public static function create($data = [], $save = true) - // { - // $conversation = new Conversation(); - // $conversation->fill($data); - - // if ($save) { - // $conversation->save(); - // } - // } + if ($save) { + $this->save(); + } + } } diff --git a/database/migrations/2022_12_17_010101_add_meta_column_to_conversations_table.php b/database/migrations/2022_12_17_010101_add_meta_column_to_conversations_table.php new file mode 100644 index 00000000..732efa98 --- /dev/null +++ b/database/migrations/2022_12_17_010101_add_meta_column_to_conversations_table.php @@ -0,0 +1,33 @@ +text('meta')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('customers', function (Blueprint $table) { + $table->dropColumn('meta'); + }); + } +}