# Use model with custom Attributes
Before using the CustomAttribute trait, you need to set it up.
- Create a configuration file inside the Category package's Config directory named attribute_entity_types.php.
- Add the following code snippet to the configuration file:
<?php
return [
'categories' => [
'name' => 'Categories',
'repository' => 'Webkul\Category\Repositories\CategoryRepository',
],
];
- Register the Configuration File:
In the CategoryServiceProvider, register the configuration file inside the register method of the provider.
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(dirname(__DIR__) . '/Config/attribute_entity_types.php', 'attribute_entity_types');
}
Now you can use the CustomAttribute
traits, which allows any model to be customized with attributes.
Example Are Give below.
<?php
namespace Webkul\Category\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Attribute\Traits\CustomAttribute;
use Webkul\Category\Contracts\Category as CategoryContract;
class Category extends Model implements CategoryContract
{
use CustomAttribute;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'slug',
'description',
];
}
In the package development repository section, we use CategoryRepository. In this repository, we inject AttributeRepository to interact with attributes. The CategoryRepository has two methods, create and update. After creating or updating a category, a new entry is created in the attribute repository using the following code:
$this->attributeValueRepository->save($data, $id);
For Example.
/**
* Create the new Resource.
*
* @param array $data
* @return \Webkul\Product\Contracts\Product
*/
public function create(array $data)
{
$product = parent::create($data);
$this->attributeValueRepository->save($data, $product->id);
return $product;
}
/**
* Update the new existing Resource.
*
* @param array $data
* @param int $id
* @param string $attribute
* @return \Webkul\Product\Contracts\Product
*/
public function update(array $data, $id, $attribute = "id")
{
$product = parent::update($data, $id);
$this->attributeValueRepository->save($data, $id);
return $product;
}