项目作者: HalalSoft

项目描述 :
Provides a trait for Laravel Eloquent/Query builder to handle MariaDB Dynamic Columns
高级语言: PHP
项目地址: git://github.com/HalalSoft/laravel-dynamic-column.git
创建时间: 2021-02-12T06:42:14Z
项目社区:https://github.com/HalalSoft/laravel-dynamic-column

开源协议:MIT License

下载


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.

  1. // The `Author` class uses the `HasDynamicColumn` trait and `Dynamic` cast attribute on the `option` column
  2. $author = Author::where('option->vehicle','car')->first();
  3. $author = $author->option;
  4. // => Array containing `option` dynamic column
  5. $option = $author->option;
  6. $option['vehicle_brand'] = 'Esemka';
  7. $author->option = $option;
  8. $author->save();
  9. //You can also create data field as array
  10. $newData = MyModel::create([
  11. 'other_column' => 'this just another column data',
  12. 'the_column' => ['data1'=>'value1','data2'=>'value2']
  13. ]);
  14. //to update a json field/key you use, you may use the `->` operator when calling the update method:
  15. $page->update(['content->data1' => 'value1new']);
  16. //or you can still update whole column using normal array:
  17. $page->update(['content' => ['data1'=>'value1new','data2'=>'value2new']]);
  18. //You can set as array using other method like `updateOrCreate()`, `firstOrCreate()`, etc.
  19. //This package also support query builder using:
  20. Model::query()->where('the_column->data1', 'value1')->first();

Install

You can install the package via composer:

  1. 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.

  1. use Illuminate\Database\Eloquent\Model;
  2. use Halalsoft\LaravelDynamicColumn\Dynamic;
  3. use Halalsoft\LaravelDynamicColumn\HasDynamicColumn;
  4. class Post extends Model
  5. {
  6. use HasDynamicColumn;
  7. protected $casts
  8. = [
  9. 'content' => Dynamic::class,
  10. ];
  11. }

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.