Skip to content

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

License

Notifications You must be signed in to change notification settings

HalalSoft/laravel-dynamic-column

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trait to Manage an MariaDB dynamic columns blob

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 [email protected] .

License

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