Closed joaolisboa closed 3 years ago
Well, nevermind my stupidity since there's a collection property for when creating attributes. I still have an issue though.
Testing in artisan tinker, and when I push a value and save the model, it doesn't update the collection. Example output from tinker:
>>> $product = Modules\Catalog\Models\Product::find(2);
=> Modules\Catalog\Models\Product {#3154
id: 2,
sku: "test",
name: "test",
family_id: 1,
product_brand_id: 1,
product_state_id: 1,
created_at: null,
updated_at: null,
deleted_at: null,
product_segment_id: 1,
product_manager: 1,
photo: null,
attribute-1: Rinvex\Attributes\Models\Type\Integer {#3184
id: 1,
content: 23,
attribute_id: 21,
entity_id: 2,
entity_type: "Modules\Catalog\Models\Product",
created_at: "2018-06-06 23:46:22",
updated_at: "2018-06-06 23:46:22",
},
attribute-1-fixed: null,
attribute-1-1: null,
description: Rinvex\Attributes\Models\Type\Varchar {#3194
id: 1,
content: "descrição produto #2",
attribute_id: 24,
entity_id: 2,
entity_type: "Modules\Catalog\Models\Product",
created_at: null,
updated_at: null,
},
file: null,
file-1: null,
option: App\Models\Option {#3216
id: 1,
content: 2,
attribute_id: 27,
entity_id: 2,
entity_type: "Modules\Catalog\Models\Product",
created_at: "2018-06-06 23:46:22",
updated_at: "2018-06-06 23:46:22",
},
options: Rinvex\Attributes\Support\ValueCollection {#3228
all: [
App\Models\Options {#3229
id: 1,
content: 1,
attribute_id: 29,
entity_id: 2,
entity_type: "Modules\Catalog\Models\Product",
created_at: "2018-06-07 00:08:39",
updated_at: "2018-06-07 00:08:39",
},
],
},
}
>>> $product->options->push("2")
=> Illuminate\Support\Collection {#3077
all: [
"1",
"2",
],
}
>>> $product->options
=> Illuminate\Support\Collection {#3141
all: [
"1",
],
}
>>> $product->save()
=> true
>>> $product->options
=> Illuminate\Support\Collection {#3225
all: [
"1",
],
}
This has been completely refactored in a different branch here: https://github.com/rinvex/laravel-attributes/tree/refactor-to-native-laravel-relationships
In the new refactor, it actually uses normal relationships, and should be easy and straightforward like default Laravel relationships. Although, that refactor is incomplete. Any help with that branch would be much appreciated! 🙂
I'm having trouble working with the ValueCollection. The documentation doesn't help because it doesn't specify in any way how the ValueCollection is supposed to be used for multi-value attributes other than how to store the values with push() and add(). I thought it'd work by creating a custom attribute but instead of Value it'd instance ValueCollection but Laravel throws an undefined Relation error. Can you help with undestanding the ValueCollection?