RomainB
3 months ago
1169
4
Feedback

Store and update request are the same except the unique id. A way to merge?

Posted 3 months ago by RomainB

Hi all, I'm creating my first full project(blog).

With the obsession to write DRY code, I created a CategoryStoreUpdateRequest (and not a StoreRequest + a UpdateRequest) to control the rules to pass and messages to return when store or update route is needed.

I'm using this personnal trick to do this:

public function rules()
    {
        $name_rules = ($this->method() == 'PATCH') // else == 'POST'
            ? 'required|string|max:50|unique:categories,name,'.$this->category->id
            : 'required|string|max:50|unique:categories,name';

        return [
            'name' => $name_rules,
            'parent_id' => 'nullable'
        ];
    }

I just want to know if some of you is seeing an issue to do this that way??

Please sign in or create an account to participate in this conversation.