PHP code example of centrex / laravel-model-data

1. Go to this page and download the library: Download centrex/laravel-model-data 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/ */

    

centrex / laravel-model-data example snippets


use Centrex\ModelData\Concerns\HasModelData;

class Product extends Model
{
    use HasModelData;
}

$product = Product::create(['name' => 'Widget']);

// Assign attributes that don't exist as columns
$product->color = 'red';
$product->weight = 1.5;
$product->is_featured = true;

$product->save();

$product = Product::find(1);

echo $product->color;       // red
echo $product->weight;      // 1.5
echo $product->is_featured; // true

$product->putData('settings', [
    'currency' => 'USD',
    'stock_alert' => true,
]);

$product->putData('seo', [
    'title' => 'Best Product',
    'description' => 'Top quality item',
]);

$settings = $product->getData('settings');

$currency = $product->getDataValue('settings', 'currency');

$product->hasData('settings');     // true
$product->forgetData('settings');  // deletes record

$column = (new Product())->getColumnForQuery('color');

Product::whereRaw("{$column} = ?", ['red'])->get();

protected $casts = [
    'is_featured' => 'boolean',
    'tags' => 'array',
];

$product->color = 'red';

$product->putData('settings', [...]);

$product->putData('pricing', [...]);
$product->putData('inventory', [...]);
$product->putData('analytics', [...]);

$product->getDataValue('settings', 'notifications.email');