rtconner / laravel-tagging

Tag support for Laravel Eloquent models - Taggable Trait
MIT License
882 stars 168 forks source link

Specify tag_group_id at Tag::$fillable #203

Closed kossmoss closed 1 year ago

kossmoss commented 2 years ago

Hello. I like the way this package organized, seems it is the most suitable for my intentions. But I encountered with some problems when tried to use it the way I usually build my applications. Most of my API methods vastly use passing IDs of categories, groups, etc. while creating a new entity.

I tried to use Tag::create() with specified tag_group_id and found this field is not present at $fillable. I didn't like the idea of passing the group name or slug instead of it's ID. Also didn't like the way of loading TagGroup from DB by specified tag_group_id and after that pass its' name to the Tag->setGroup() (I also see setGroup() can load other group with the same slug, but this is not an issue I am talking here). For the moment I bypassed the existing logic with this:

    public function store(StoreTagRequest $request)
    {
        $tag = Tag::query()->create($request->validated());
        if ($groupId = $request->input('tag_group_id')) {
            $tag->mergeFillable(['tag_group_id'])->update(['tag_group_id' => $groupId]);
        }

        $tag->load('group');
        return $this->sendJsonResponse($tag);
    }

The main question: Is there any reason to protect tag_group_id field from assigning right at update() or create()? Something not obvious like any hidden logic about that I do not see at the moment (for example, when record is updated or something like this)?

kossmoss commented 2 years ago

Just found the same problem with the suggest field. Seems like this one definitely can be added to $fillable when create/update the tag

kossmoss commented 2 years ago

Seems like finally I've got why these fields are not fillable out of the box :grin:. This is more suitable when users specify tags by themselves.