GithubHelp home page GithubHelp logo

davidwesdijk / laravel-polymorphic-eav Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 4.0 17 KB

Integrate the entity-attribute-value model with polymorphic Eloquent-relations in your Laravel application.

License: MIT License

PHP 100.00%
php laravel eav eav-model entity-attribute entity-attribute-value eloquent

laravel-polymorphic-eav's Introduction

Polymorphic EAV-model integration

Latest Version on Packagist Total Downloads

Integrate the entity-attribute-value model with polymorphic Eloquent-relations in your Laravel application.

Installation

You can install the package via composer:

composer require davidwesdijk/laravel-polymorphic-eav

Usage

Preparing your models

In order to implement the Entity-Attribute-Value-model to your Eloquent models, the only required action is to add the HasEntityAttributeValues Trait. That's all action required to use the full features this package brings.

use DavidWesdijk\LaravelPolymorphicEav\Traits\HasEntityAttributeValues;

class Product extends Model
{
     use HasEntityAttributeValues;

    // Implementation of your Eloquent model
}

Assigning attributes

Attributes are, when the Trait is added, accessable and assignable by the props accessor on your Eloquent Model. Just set its value!

class ProductController extends Controller
{
    public function update(AnyValidatedRequest $request, Product $product)
    {
        $product->props->details->color = $request->color; // E.g. 'red'

        // That's it!
    }
}

Accessing attributes

The assigned attributes are simply accessible by defining the group and the attribute in the props accessor like the following example. The concept is that all required properties must be accessible, without throwing exceptions, to keep you code clean, readable and maintainable.

// This attribute has been set before
$product->props->details->color; // 'red'

// This attribute has never been set before 
$product->props->details->sku; // null

// Even if the group has never been defined 
$product->props->foo->bar; // null

Other functionality

If you'd like e.g. remove a property you can either set it's value to null, or you can simply unset the value. The database entry will be removed.

unset($product->props->details->color);

$product->props->details->color; // null

Serializing

You might run into a situation where you have to have all assigned properties available in an array or json. This package respects the Arrayable and Jsonable interfaces.

$product->props->details->toArray(); ['color' => 'red']
$product->props->details->toJson(); '{color: "red"}'

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-polymorphic-eav's People

Contributors

davidwesdijk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-polymorphic-eav's Issues

Config is not deployed, migration crashes

in the migration file we can find the following:

Schema::create(config('laravel-polymorphic-eav.attribute-table'), function (Blueprint $table) { and Schema::create(config('laravel-polymorphic-eav.value-table'), function (Blueprint $table) {

however, the README.md does not states how to export the config file included (php artisan vendor:publish --tag )
another viable solution would be to have the config use the second parameter for a default value.

Im willing to create a PR if you want :-)

Update to PHP 8

Hi, I noticed that the package doesn't support PHP 8 yet. Are you by any chance planning to update the package to PHP 8?

Relations

How to get attributes from all products

For example, like this

Product::withAttributes()->with('category')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.