PHP code example of roundly-consulting / attributes-for-laravel

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

    

roundly-consulting / attributes-for-laravel example snippets


use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Attributes\Contracts\HasAttributes as HasAttributesContract;
use RoundlyConsulting\Attributes\Traits\HasAttributes;

class Product extends Model implements HasAttributesContract
{
    use HasAttributes;
}

use RoundlyConsulting\Attributes\DataTransferObjects\AttributeDefinitionData;
use RoundlyConsulting\Attributes\Enums\AttributeType;
use RoundlyConsulting\Attributes\Facades\Attributes;

Attributes::define(new AttributeDefinitionData(
    name: 'rating',
    type: AttributeType::Integer,
    rules: ['min:1', 'max:5'],
));

Attributes::for($product)->set('color', 'red', meta: ['hex' => '#f00']);
Attributes::for($product)->setMany(['rating' => '5', 'on_sale' => true]);   // '5' is stored as 5

Attributes::for($product)->get('rating');     // 5
Attributes::for($product)->toKeyValue();      // ['color' => 'red', 'rating' => 5, 'on_sale' => true]
Attributes::for($product)->set('rating', 9);  // throws InvalidAttributeValueException

Product::query()->whereAttribute('color', 'red')->whereAttributeBetween('rating', 4, 5)->get();
bash
composer vendor:publish --tag="attributes-migrations"
php artisan migrate