PHP code example of halalsoft / laravel-dynamic-column

1. Go to this page and download the library: Download halalsoft/laravel-dynamic-column library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

halalsoft / laravel-dynamic-column example snippets


// 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();

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

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