thebachtiarz / laravel-additional-attribute-v1
A Simple Additional Attribute for Laravel Project v1
2.0.8
2022-10-26 21:36 UTC
Requires
- php: ^8.0
- laravel/framework: ^9.0
- thebachtiarz/laravel-toolkit-v1: ^2.0
README
A Simple Additional Attribute for Laravel Project v1
Requires
Installation
- composer config (only if you have access)
composer config repositories.thebachtiarz/laravel-additional-attribute-v1 git git@github.com:thebachtiarz/laravel-additional-attribute-v1.git
- install repository
composer require thebachtiarz/laravel-additional-attribute-v1
- vendor publish
php artisan vendor:publish --provider="TheBachtiarz\AdditionalAttribute\ServiceProvider"
- database migration
php artisan migrate
Implementation
- add Class Trait Service below here into Model.
use \TheBachtiarz\AdditionalAttribute\Service\AdditionalAttributes;
- Example:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { use \TheBachtiarz\AdditionalAttribute\Service\AdditionalAttributes; }
Feature
Create new attribute or Update existing attribute in model.
/** * Create or update attribute * * @param string $attrName * @param mixed $attrValue * @return AdditionalAttribute|null */ public function setAttr(string $attrName, $attrValue): ?AdditionalAttribute;
App\Models\User::find(1)->setAttr('attrName', 'attrValue');
Get attribute in model by attribute name.
/** * Get attribute by name * * @param string $attrName * @param boolean $map default: false * @return mixed */ public function getAttr(string $attrName, bool $map = false): mixed;
App\Models\User::find(1)->getAttr('attrName', false);
Get attribute in model only value by attribute name.
/** * Get attribute by name. * Get only value. * * @param string $attrName * @param boolean $withKey default: false * @return mixed */ public function getAttrValue(string $attrName, bool $withKey = false): mixed;
App\Models\User::find(1)->getAttrValue('attrName', false);
Get all attribute(s) in model.
/** * Get all attribute belongs to model * * @param boolean $map default: false * @return array|null */ public function getAttrs(bool $map = false): ?array;
App\Models\User::find(1)->getAttrs(false);
Search value by attribute name.
/** * Search value by attribute name * * @param string $attrName * @param string $valueToSearch * @param boolean $map * @return array */ public static function searchValueByAttr(string $attrName, string $valueToSearch, bool $map = false): array;
App\Models\User::searchValueByAttr('attributeName', 'valueToSearch', false);