PHP code example of balajidharma / laravel-attributes

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

    

balajidharma / laravel-attributes example snippets



namespace BalajiDharma\LaravelForum\Models;

use BalajiDharma\LaravelAttributes\Traits\HasAttributable;
use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    use HasAttributable;
	

$thread->save();

$thread->attachAttribute('color', 'red');



$thread->attachAttribute('color', 'red', 'string');

$thread->attachAttribute('price', '10', 'integer');

$thread->attachAttribute('is_active', '1', 'boolean');



$thread->attachAttribute('color', 'red', 'string', 1);

$thread->attachAttribute('price', '10', 'integer', 2);

$thread->attachAttribute('is_active', '1', 'boolean', 3);


$data = [
    [
        'name' => 'color',
        'value' => 'red',
        'data_type' => 'string'
    ],
    [
        'name' => 'price',
        'value' => '10',
        'data_type' => 'interger'
    ],
    [
        'name' => 'is_active',
        'value' => '1',
        'data_type' => 'boolean'
    ],
]

$thread->attachAttributes($data);


$thread = Thread::query()->with('attributes')->get();

$thread->attributes;

if ($thread->hasAttributeValue('red')) {
    return 'attribute value';
}

return 'no attribute value';

if ($thread->hasAttributName('color')) {
    return 'attribute name';
}

return 'no attribute name';

if ($thread->hasAttributDataType('json')) {
    return 'attribute data type';
}

return 'no attribute data type';

// Fetch threads with their related attributes
$thread = Thread::query()->with('attributes')->get();

// Access attribute data
foreach ($thread->attributes as $attribute) {
    echo $attribute->data;
}

$thread->deleteAllAttribute();

$thread->deleteAttribute('color', 'red');

$thread->deleteAttributeByName('color');

$thread->deleteAttributeByValue('red');

$thread->deleteAttributeByDataType('string');

'models' => [
    'attributes' => BalajiDharma\LaravelAttributes\Models\Attributes::class,
],

'table_names' => [
    'attributes' => 'attributes',
],


'validate_value_before_save' => true,


'data_types' => [
    ['name' => 'string', 'validation' => 'string', 'cast' => 'string'],
    ['name' => 'integer', 'validation' => 'integer', 'cast' => 'integer'],
    ['name' => 'float', 'validation' => 'numeric', 'cast' => 'float'],
    ['name' => 'boolean', 'validation' => 'boolean', 'cast' => 'boolean'],
    ['name' => 'date', 'validation' => 'date', 'cast' => 'date'],
    ['name' => 'json', 'validation' => 'json', 'cast' => 'array'],
],
bash
php artisan vendor:publish --provider="BalajiDharma\LaravelAttributes\AttributesServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="BalajiDharma\LaravelAttributes\AttributesServiceProvider" --tag="config"