1. Go to this page and download the library: Download dmattern/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/ */
dmattern / 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;
}
// 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');
$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();
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.