balajidharma/laravel-attributes
A flexible attribute management system for Laravel models
Maintainers
Package info
github.com/balajidharma/laravel-attributes
pkg:composer/balajidharma/laravel-attributes
Requires
- illuminate/support: ^11.0|^12.0|^13.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 5c21f4631ee189755c2b91ca48f9a6a257d02e65
attributeslaravellaravel-frameworklaravel-attributeslaravel-attribute
README
A flexible attribute management system for Laravel models.
👁 Total Downloads
👁 Latest Stable Version
👁 License
Overview
Laravel Attributes allows you to add custom attributes to your Laravel models with support for different data types, sorting, and automatic casting.
Table of Contents
- Installation
- Save Attrubute
- Get Attributes
- Getting Attribute Casting Values
- Configuration
- Credits
- Demo
Installation
- Install the package via composer
composer require balajidharma/laravel-attributes
- Publish the migration with
php artisan vendor:publish --provider="BalajiDharma\LaravelAttributes\AttributesServiceProvider" --tag="migrations"
- Run the migration
php artisan migrate
- To Publish the config/attributes.php config file with
php artisan vendor:publish --provider="BalajiDharma\LaravelAttributes\AttributesServiceProvider" --tag="config"
- Preparing your model To associate views with a model, the model must implement the HasAttributes trait:
<?php namespace BalajiDharma\LaravelForum\Models; use BalajiDharma\LaravelAttributes\Traits\HasAttributable; use Illuminate\Database\Eloquent\Model; class Thread extends Model { use HasAttributable;
Save attribute
- Save single attibute
$thread->save(); $thread->attachAttribute('color', 'red');
- Save single attibute with data type
$thread->attachAttribute('color', 'red', 'string'); $thread->attachAttribute('price', '10', 'integer'); $thread->attachAttribute('is_active', '1', 'boolean');
default data type is string
- Save single attibute with weight
The weight used to sort the attributes
$thread->attachAttribute('color', 'red', 'string', 1); $thread->attachAttribute('price', '10', 'integer', 2); $thread->attachAttribute('is_active', '1', 'boolean', 3);
default weight value is 0
- Save multiple attibute
$data = [ [ 'name' => 'color', 'value' => 'red', 'data_type' => 'string' ], [ 'name' => 'price', 'value' => '10', 'data_type' => 'interger' ], [ 'name' => 'is_active', 'value' => '1', 'data_type' => 'boolean' ], ] $thread->attachAttributes($data);
weight will be added based on array index
Get Attributes
- Get attributes with query
$thread = Thread::query()->with('attributes')->get(); $thread->attributes;
- Check attribute value is exists
if ($thread->hasAttributeValue('red')) { return 'attribute value'; } return 'no attribute value';
- Check attribute name is exists
if ($thread->hasAttributName('color')) { return 'attribute name'; } return 'no attribute name';
- Check attribute data type is exists
if ($thread->hasAttributDataType('json')) { return 'attribute data type'; } return 'no attribute data type';
Getting Attribute Casting Values
You can get the casting value in data attribute
// Fetch threads with their related attributes $thread = Thread::query()->with('attributes')->get(); // Access attribute data foreach ($thread->attributes as $attribute) { echo $attribute->data; }
Delete Attributes
- Delete all attributes
$thread->deleteAllAttribute();
- Delete attribute by name and value
$thread->deleteAttribute('color', 'red');
- Delete attribute by name
$thread->deleteAttributeByName('color');
- Delete attribute by value
$thread->deleteAttributeByValue('red');
- Delete attribute by data type
$thread->deleteAttributeByDataType('string');
Laravel Attributes Configuration
This document describes all configuration options available in the attributes.php config file.
Configuration Options
Models
'models' => [ 'attributes' => BalajiDharma\LaravelAttributes\Models\Attributes::class, ],
Defines the model class used to save attributes. You can override this with your own model class if needed.
'table_names' => [ 'attributes' => 'attributes', ],
Specifies the database table name used for storing attributes. Default is 'attributes'.
Validation
'validate_value_before_save' => true,
Disable or enable the value validation based on data type.
Data Type and Casting
'data_types' => [ ['name' => 'string', 'validation' => 'string', 'cast' => 'string'], ['name' => 'integer', 'validation' => 'integer', 'cast' => 'integer'], ['name' => 'float', 'validation' => 'numeric', 'cast' => 'float'], ['name' => 'boolean', 'validation' => 'boolean', 'cast' => 'boolean'], ['name' => 'date', 'validation' => 'date', 'cast' => 'date'], ['name' => 'json', 'validation' => 'json', 'cast' => 'array'], ],
Support all the Eloquent Attribute Casting
Credits
This package is based on milwad-dev/laravel-attributes and has been modified to provide additional functionality.
Demo
The "Basic Laravel Admin Penel" starter kit come with Laravel Attributes
