pwrdk / laravel-custom-attributes
Custom Attributes Package for Eloquent Models
Requires
- php: ^7.3|^8.0
Requires (Dev)
- orchestra/testbench: ^6.0@dev
- dev-master
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.10
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-12-12 19:40:18 UTC
README
A custom attributes package for Eloquent Models.
Installation
You can install the package via composer:
composer require pwrdk/laravel-custom-attributes
Migrate
php artisan:migrate
Seed
Install basic Attribute Types (text, number, boolean, datetime and contact info)
php artisan db:seed --class="PWRDK\CustomAttributes\Seeds\DatabaseSeeder"
Publish config file
php artisan vendor:publish --provider="PWRDK\CustomAttributes\CustomAttributesServiceProvider"
Usage
Add HasCustomAttributes trait to Any Eloquent Model
use PWRDK\CustomAttributes\HasCustomAttributes; use HasCustomAttributes;
Create a new attribute key
use PWRDK\CustomAttributes\CustomAttributes; CustomAttributes::createKey('is_active', 'Is Active', 'boolean', true); CustomAttributes::createKey('last_seen', 'Last seen', 'datetime', true); CustomAttributes::createKey('favourite_colours', 'Favourite Colours', 'text', false);
List all available keys:
AttributeKey::all()->pluck('handle', 'display_name');
Attach the attribute to the Model
$user->attr()->set('is_active', true); $user->attr()->set('last_seen', now()); $user->attr()->set('favourite_colours','red'); $user->attr()->set('favourite_colours','green'); $user->attr()->set('favourite_colours','blue');
Get an attribute by handle
$user->attr()->is_active; => Illuminate\Support\Collection {#962 all: [ [ "key" => "is_active", "value" => 1, "created_at" => Illuminate\Support\Carbon @1575184861 {#956 date: 2019-12-01 07:21:01.0 UTC (+00:00), }, "id" => 56, ], ], } $user->attr()->last_seen; => Illuminate\Support\Collection {#962 all: [ [ "key" => "last_seen", "value" => Illuminate\Support\Carbon @1574796362 {#960 date: 2019-11-26 19:26:02.0 UTC (+00:00), }, "created_at" => Illuminate\Support\Carbon @1575184900 {#956 date: 2019-12-01 07:21:40.0 UTC (+00:00), }, "id" => 57, ], ], } $user->attr()->favourite_colours; => Illuminate\Support\Collection {#962 all: [ [ "key" => "favourite_colours", "value" => "orange", "created_at" => Illuminate\Support\Carbon @1575184900 {#956 date: 2019-12-01 07:21:40.0 UTC (+00:00), }, "id" => 58, "creator" => App\User {#983 id: 1, name: "Rudolf Schwann", email: "schwann@damernesmagasin.com", email_verified_at: null, created_at: "2019-11-26 08:42:25", updated_at: "2019-11-26 08:42:25", }, ], [ "key" => "favourite_colours", "value" => "green", "created_at" => Illuminate\Support\Carbon @1575184900 {#981 date: 2019-12-01 07:21:40.0 UTC (+00:00), }, "id" => 59, "creator" => App\User {#983}, ], [ "key" => "favourite_colours", "value" => "purple", "created_at" => Illuminate\Support\Carbon @1575184900 {#988 date: 2019-12-01 07:21:40.0 UTC (+00:00), }, "id" => 60, "creator" => App\User {#983}, ], ], }
Unsetting an attribute
$user->attr()->unset('is_active');
Updating an attribute is done by referencing the ID directly
$user->attr()->update(58, ['value' => 'cyan']); => PWRDK\CustomAttributes\Models\AttributeTypes\AttributeTypeDefault {#975 value: "yellow", }
Using local AttributeTypes
- Create new model called CustomAttributes in your own models directory
- make a migration for the attribute type.
art make:migration create_values_type_prices_table
Create a new attribute key type
AttributeType::create(['handle' => 'prices', 'display_name' => 'Prices in different currencies']);
Extend CustomAttributes model from the package and add the new attribute type relationships
class CustomAttribute extends CustomAttributeModel { public function attributeTypeScheduleEventOverlay() { return $this->hasOne(AttributeTypeScheduleEventOverlay::class); } }
Add the HasLocalCustomAttributeType
interface to the model(s)
class Product extends Model implements HasLocalCustomAttributeType { ... }
Set the values for the attributes
$product = App\Models\Product::find(1000); $product->attr()->set('prices', ['usd' => 100, 'eur' => 100, 'gbp' => 90, 'jpy' => 10695]);
Caching attribute values:
Add the HasLocalCustomAttributeType
interface to the model(s)
class Product extends Model implements UsesCustomAttributesCaching, HasLocalCustomAttributeType { ... }