PHP code example of tec-more / attributes

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

    

tec-more / attributes example snippets


select * from `companies`
select * from `attribute_varchar_values` where `attribute_id` = '1' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company'
select * from `attribute_varchar_values` where `attribute_id` = '2' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company'
select * from `attribute_varchar_values` where `attribute_id` = '3' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company'
select * from `attribute_varchar_values` where `attribute_id` = '4' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company'

// To build entity relations for every instance
bootIfNotBooted();

// To multivalued attributes)
setRelation()

// To let Eloquent use our attribute relations as part of the model
getRelationValue()

// This is how it works
$entity->cities->add('Alexandria');

// And this is what you would have to do without this collection:
$value = new Varchar(['content' => 'Alexandria', 'attribute_id' => 1, 'entity_type' => 'App\Models\Company', 'entity_id' => 1]);
$entity->cities->push($value);

// You could also pass an array
$entity->cities->add(['Alexandria', 'Cairo']);

class Company extends Model
{
    use \Rinvex\Attributes\Traits\Attributable;
}

app('rinvex.attributes.types')->push(\Path\To\Your\Type::class);

app('rinvex.attributes.entities')->push(\Path\To\Your\Entity::class);

app('rinvex.attributes.attribute')->create([
    'slug' => 'size',
    'name' => 'Product Size',
    'type' => 'Rinvex\Attributes\Models\Type\Varchar',
    'entities' => ['App\Models\Company', 'App\Models\Product'],
]);

$attribute = app('rinvex.attributes.attribute')->find(1);

// Get attribute entities collection
$attribute->entities

// Get attribute entities query builder
$attribute->entities();

// Delete attached attribute entities
$attribute->entities()->delete();

// Attach attribute entities
$attribute->entities()->createMany([
    [...],
    [...],
    [...],
]);

// Alternative way of attaching attribute entities
$attribute->fill([
    'entities' => ['App\Models\Company', 'App\Models\Product'],
])->save();

// Get all attribute values of type varchar
$values = $attribute->values('Rinvex\Attributes\Models\Type\Varchar')->get();

// City is an entity attribute
$companies = Company::whereHas('city', function (\Illuminate\Database\Eloquent\Builder $builder) {
    $builder->where('content', 'Alexandria');
})->get();

$companies = Company::hasAttribute('city', 'Alexandria')->get();

$company = Company::find(1);

// Get entity attributes
$company->cities;

// Get entity raw relation
$company->cities();

$company->load('eav');
$company->load('city', 'colors');

namespace App\Models;

use Rinvex\Attributes\Traits\Attributable;

class Company extends Model
{
    use Attributable;

    // Eager loading all the registered attributes
    protected $with = ['eav'];

    // Or just load a few of them
    protected $with = ['city', 'colors'];
}
shell
    php artisan rinvex:migrate:attributes