Created
May 13, 2018 02:51
-
-
Save alvin4frnds/ab38707d5ace504a0b535e1f9b8ca6fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Trait for handling serialized columns for laravel projects | |
*/ | |
namespace App\Traits; | |
trait HaveSerializedColumns | |
{ | |
public function getMetaAttribute($value) | |
{ | |
return $this->unserialized($value); | |
} | |
public function setMetaAttribute($value) | |
{ | |
$this->attributes['meta'] = $this->serialized($value); | |
} | |
public function getSettingsAttribute($value) | |
{ | |
return $this->unserialized($value); | |
} | |
public function setSettingsAttribute($value) | |
{ | |
$this->attributes['settings'] = $this->serialized($value); | |
} | |
private function unserialized($value) | |
{ | |
if (! $value) return []; | |
if (is_string($value)) return unserialize($value); | |
return (array) $value; | |
} | |
private function serialized($value) | |
{ | |
if (! $value) return serialize([]); | |
if (is_string($value)) return $value; | |
return serialize($value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment