janmoes
1 month ago
231
5
Laravel

Validating combination of two fields

Posted 1 month ago by janmoes

Hi there,

I'm having trouble with validation. I want a post_translation to have a unique combination of a post_id and lang_id. I've tried some stuff from the docs but didn't get it to work.

If a translation already uses a language or not, it keeps giving the error message "Lang id already in use". I've made the validation in a custom form request:

public function rules (Request $request) 
    {
        return [
            'lang_id' => ['required', Rule::unique('post_translations')->where(function ($query) {
                return $query->where('post_id', 1);
            })],
            'title' => 'required',
            'content' => 'required',
        ];
    }

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