rinvex / laravel-attributes

⚠️ [ABANDONED] Rinvex Attributable is a robust, intelligent, and integrated Entity-Attribute-Value model (EAV) implementation for Laravel Eloquent, with powerful underlying for managing entity attributes implicitly as relations with ease. It utilizes the power of Laravel Eloquent, with smooth and seamless integration.
MIT License
433 stars 104 forks source link

Deleting Attributes? #65

Closed dhildreth closed 5 years ago

dhildreth commented 6 years ago

I'm struggling with building a UI for creating, editing, and deleting attributes. Specifically, I'm stuck on deleting the attributes from the entity (product).

I imagine it should be something as simple as a foreach loop to remove/unset all attributes:

    function syncAttributes(Product $product, ProductRequest $request)
   {
        foreach($product->getEntityAttributes() as $key => $value) {
            $product->{$key} = null;
        }
        $product->save();

However, that doesn't work and I'm left with this monstrosity of a solution:

        foreach($product->getEntityAttributes() as $key => $value) {
            $product->{$key} = null;
            $product->save();
            Cache::flush();
            $product = Product::find($product->id);
        }

I'm hoping this is one of those moments where I'm just missing something simple. I could use a hand please!

heydluv commented 6 years ago

Have you tried doing $attribute->delete(); as shown here on line 240?

dhildreth commented 6 years ago

Thanks for the response, but that will actually delete the attribute entirely. I still want to keep the attribute for the entity, but I want to 'null' it out.

dhildreth commented 5 years ago

It probably deserves more testing, but this seems to do the trick:

$product->{$key}()->delete();