halalsoft/laravel-dynamic-column

Provides a trait for Laravel Eloquent/Query builder to handle MariaDB Dynamic Column.

v0.1.3 2022-08-29 20:48 UTC

This package is auto-updated.

Last update: 2024-10-29 06:06:09 UTC


README

Latest Version on Packagist Software License Quality Score Total Downloads

The laravel-dynamic-column package provides a HasDynamicColumn trait, which allows you to easily handle MariaDB dynamic column using Eloquent or Query Builder.

// The `Author` class uses the `HasDynamicColumn` trait and `Dynamic` cast attribute on the `option` column
$author = Author::where('option->vehicle','car')->first();


$author = $author->option;
// => Array containing `option` dynamic  column
$option = $author->option;
$option['vehicle_brand'] = 'Esemka';
$author->option = $option;
$author->save();

//You can also create data field as array
$newData = MyModel::create([
    'other_column' => 'this just another column data',
    'the_column' => ['data1'=>'value1','data2'=>'value2']
]);

//to update a json field/key you use, you may use the `->` operator when calling the update method:

$page->update(['content->data1' => 'value1new']);
    
//or you can still update whole column using normal array:
$page->update(['content' => ['data1'=>'value1new','data2'=>'value2new']]);
//You can set as array using other method like `updateOrCreate()`, `firstOrCreate()`,  etc.

//This package also support query builder using:
Model::query()->where('the_column->data1', 'value1')->first();

Install

You can install the package via composer:

composer require halalsoft/laravel-dynamic-column

Usage

You can start using the package by adding the HasDynamicColumn trait and use Dynamic as attribute cast to your models.

use Illuminate\Database\Eloquent\Model;
use Halalsoft\LaravelDynamicColumn\Dynamic;
use Halalsoft\LaravelDynamicColumn\HasDynamicColumn;

class Post extends Model
{
    use HasDynamicColumn;
    protected $casts
        = [
            'content' => Dynamic::class,
        ];
}

Other explain will be added soon

Security

If you discover any security related issues, just open an issue on this git or email me to dyas@yaskur.com .

License

The MIT License (MIT). Please see License File for more information.