PHP code example of denismitr / laravel-json-attributes

1. Go to this page and download the library: Download denismitr/laravel-json-attributes 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/ */

    

denismitr / laravel-json-attributes example snippets


Schema::create('orders', function (Blueprint $table) {
    $table->increments('id');
    $table->string('description');
    $table->jsonData('json_data');
});

$array = [
    'supplier' => 'Boeing',
    'total_cost' => 245.99,
];

$record = Record::create(['json_data' => $array]);

$this->assertEquals($array, $record->json_data->all());
$this->assertEquals('Boeing', $record->json_data->supplier);
$this->assertEquals('Boeing', $record->json_data['supplier']);

$this->record->json_data->member = ['name' => 'John', 'age' => 30];
$this->record->json_data->forget('member.age');
$this->assertEquals($this->record->json_data->member, ['name' => 'John']);

$this->record->json_data->set('settings.connection', 'mysql');
$this->record->json_data->set('colors.navbar', 'dark');

$this->assertEquals('mysql', $this->record->json_data->get('settings.connection'));
$this->assertEquals('dark', $this->record->json_data->get('colors.navbar'));

$array = [
    'one' => 'value',
    'two' => 'another value',
];

$this->record->json_data->array = $array;

$this->assertEquals($array, $this->record->json_data->array);

$recordA = Record::create(['json_data' => [
    'company' => 'Ecommelite',
    'user' => [
        'name' => 'Denis',
        'job_title' => 'developer'
    ]
]]);

$recordB = Record::create(['json_data' => [
    'company' => 'Ecommelite',
    'user' => [
        'name' => 'Tom',
        'job_title' => 'developer'
    ]
]]);

$recordC = Record::create(['json_data' => [
    'company' => 'Ecommelite',
    'address' => [
        'street' => '1st Street',
        'phone' => 1234556
    ]
]]);

$this->assertContainsModels(
    [$recordA],
    Record::withJsonData(['user.name' => 'Denis'])->get()
);

$this->assertContainsModels(
    [$recordA, $recordB],
    Record::withJsonData(['user.job_title' => 'developer'])->get()
);

$this->assertContainsModels(
    [$recordA, $recordB],
    Record::withJsonData(['user.job_title' => 'developer', 'company' => 'Ecommelite'])->get()
);

$this->assertContainsModels(
    [$recordA],
    Record::withJsonData([
        'user.job_title' => 'developer',
        'company' => 'Ecommelite',
        'user.name' => 'Denis'
    ])->get()
);

$this->assertContainsModels(
    [$recordA, $recordB],
    Record::withJsonData('user.job_title', 'developer')->get()
);

$this->assertContainsModels(
    [], Record::withJsonData(['non.existent' => 'record'])->get()
);

use Denismitr\JsonAttributes\JsonAttributes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Record extends Model
{
    protected $casts = ['json_data' => 'array'];

    /**
     * @return JsonAttributes
     */
    public function getJsonDataAttribute(): JsonAttributes
    {
        return JsonAttributes::create($this, 'json_data');
    }

    /**
     * @return Builder
     */
    public function scopeWithJsonData(): Builder
    {
        return JsonAttributes::scopeWithJsonAttributes('json_data');
    }
}

'providers' => [
    // ...
    Denismitr\JsonAttributes\JsonAttributesServiceProvider::class,
];