sunel / eav

Entity–attribute–value model (EAV) for Laravel Artisan
https://sunel.github.io/eav/
143 stars 39 forks source link

Error while getting the attribute options values #26

Closed joaopmmartins closed 5 years ago

joaopmmartins commented 5 years ago

When I run the code:

$statusAttr = Eav\Attribute::with('optionValues')->findByCode('size', 'product');

I get the following error:

BadMethodCallException Call to undefined method Illuminate\Database\Eloquent\Builder::findByCode()

sunel commented 5 years ago

@joaopmmartins

At this time this is not possible. findByCode is a static method on Eav\Attribute.

You can make use of https://laravel.com/docs/5.7/eloquent-relationships#lazy-eager-loading

$statusAttr = Eav\Attribute::findByCode('size', 'product');

$statusAttr->load('optionValues');

// You must use this to access the options.
$statusAttr->options();

I am planning to convert this a scoped query.

sunel commented 5 years ago

@joaopmmartins

I have updated the doc, sorry for the wrong update in the doc, the right way is to use as mention in the comments