Created
July 17, 2025 08:42
-
-
Save alaminfirdows/21aa1913805456cb042f22931b783860 to your computer and use it in GitHub Desktop.
Optimizing JSON columns in Laravel
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
namespace App\Models\Concerns; | |
trait HasVirtualFields | |
{ | |
public function save(array $options = []) | |
{ | |
if (isset($this->virtualFields)) { | |
$this->attributes = array_diff_key($this->attributes, array_flip($this->virtualFields)); | |
} | |
return parent::save($options); | |
} | |
} |
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
$table->string('approved_at') | |
->nullable() | |
->virtualAs("json_unquote(json_extract(data, '$.latest_approval_date'))"); | |
$table->index('approved_at'); |
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
use App\Models\Concerns\HasVirtualFields; | |
class Document extends Model | |
{ | |
use HasVirtualFields; | |
protected array $virtualFields = [ | |
'approved_at', | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment