PHP code example of kjmtrue / laravel-attributes

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

    

kjmtrue / laravel-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 multi-valued attributes)
setRelation()

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

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

\Rinvex\Attributes\Models\Type\Text::class
\Rinvex\Attributes\Models\Type\Boolean::class
\Rinvex\Attributes\Models\Type\Integer::class
\Rinvex\Attributes\Models\Type\Varchar::class
\Rinvex\Attributes\Models\Type\Datetime::class

use Rinvex\Attributes\Models\Attribute;

Attribute::typeMap([
    'varchar' => Rinvex\Attributes\Models\Type\Varchar::class,
    // ...
    'custom' => \Path\To\Your\Type::class,
]);

// Push your entity fully qualified namespace
app('rinvex.attributes.entities')->push(\Path\To\Your\Entity::class);

// Or push the morph class alias if any
app('rinvex.attributes.entities')->push('entity');

app('rinvex.attributes.attribute')->create([
    'slug' => 'size',
    'type' => 'varchar',
    'name' => 'Product Size',
    '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('varchar')->get();

// Single value assignment
$product = \App\Models\Product::find(1);
$product->price = 123;
$product->save();

// Mass assignment
$product = \App\Models\Product::find(1);
$product->fill(['price' => 123])->save();

// 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']);

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

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

$company = Company::find(1);

// Get entity attributes
$company->cities;

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

$company->load('eav');
$company->load('cities', '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 = ['cities', 'colors'];
}
shell
    php artisan rinvex:publish:attributes
    
shell
    php artisan rinvex:migrate:attributes