IsraelOrtuno / Eavquent

EAV modeling package for Laravel and Eloquent – This package is abandoned, please use this fork https://github.com/rinvex/attributes
63 stars 13 forks source link

update or add new attribute #44

Closed breaktag closed 6 years ago

breaktag commented 8 years ago

Hi,

I wondered if there is an easy way to allow my users to create their own attributes? I would ideally like to see a check inside the setAttribute() method to check whether the attribute exists and if not create it.

It seems a long winded process for me to add a check in everytime I want to use a certain attribute. And in my case, once it is created once, I will have a thousand members be able to use it.

A bit of background for my case. I am building a membership management system for a sports club. There are 1000 members, and I have around 100 different custom fields to implement.

If anyone can assist that would be great.

Many thanks

IsraelOrtuno commented 8 years ago

This sounds like a new feature, let's say we want to just assign an attribute that does not exist yet... I think with some kind of Factory we could identify what's the data type of the given value and create an attribute if does not exist... Will consider. Working on the package.

breaktag commented 8 years ago

Okay great. Secondly, (apologies for all the question!) is there a way for me to load all the attributes as objects for a given entity? I'd like to generate a form which shows a list of the attribute labels along a form field input/text area/checkbox based on the attribute type. I can see the key/value pairs which get loaded into the relations field of my entity model however require the full attribute object/model as well.

Thanks

IsraelOrtuno commented 8 years ago

I think this is what you are looking for:

https://github.com/IsraelOrtuno/Eavquent/blob/master/tests/integration/EavquentTestTrait.php#L80-L86

If you user raw{Attribute}Object name, Eavquent will provide the model object.

breaktag commented 8 years ago

Thank you.

I thought I would share my solution as it may help others. And I will also be doing a similar loop through to save all the attributes as well instead of manuall doing setAttribute('attribute_name') for each one. ` foreach($model->getEntityAttributes() as $attribute)

                    <div class="form-group">
                        {!! Form::label($attribute->name, $attribute->label, ['class' => 'col-sm-2 control-label']) !!}
                        <div class="col-sm-10">
                            <?php
                            $classPath = explode('\\', $attribute->type);
                            $type = end($classPath);

                            switch($type)
                            {
                                case 'Varchar':
                                    echo Form::text($attribute->name, old($attribute->name, $model->{$attribute->name}), ['class' => 'form-control']);
                                    break;
                                case 'Text':
                                    echo Form::textarea($attribute->name, old($attribute->name, $model->{$attribute->name}), ['class' => 'form-control']);
                                    break;
                                case 'Integer':
                                    echo Form::number($attribute->name, old($attribute->name, $model->{$attribute->name}), ['class' => 'form-control']);
                                    break;
                                case 'Floatnum':
                                    echo Form::number($attribute->name, old($attribute->name, $model->{$attribute->name}), ['class' => 'form-control']);
                                    break;
                                case 'Datetime':
                                    echo '<div class="input-group date"><div class="input-group-addon"><i class="fa fa-calendar"></i></div>';
                                    echo Form::text($attribute->name, old($attribute->name, $model->{$attribute->name}), ['class' => 'datepicker form-control']);
                                    echo '</div>';
                                    break;
                                case 'Boolean':
                                    echo Form::checkbox($attribute->name, 1, old($attribute->name, $model->{$attribute->name}), ['class' => 'flat-green']);
                                    break;
                                default:
                                    echo Form::text($attribute->name, old($attribute->name, $model->{$attribute->name}), ['class' => 'form-control']);
                                    break;
                            }
                            ?>
                        </div>
                    </div>
                endforeach

`