Laravel Attributes allows you to add custom attributes to your Laravel models with support for different data types, sorting, and automatic casting.
- Installation
- Save Attrubute
- Get Attributes
- Getting Attribute Casting Values
- Configuration
- Credits
- Demo
- 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 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 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';
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 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');
This document describes all configuration options available in the attributes.php
config file.
'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'.
'validate_value_before_save' => true,
Disable or enable the value validation based on data type.
'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
This package is based on milwad-dev/laravel-attributes and has been modified to provide additional functionality.
The "Basic Laravel Admin Penel" starter kit come with Laravel Attributes