Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

xalafnasirov's avatar

Validation doesn't work

Hello

		public function edit(Request $request) { 

		try {

        $request->validate([
            'id' => 'exists:languages,lang_key'
        ]);

        $exists = DB::table('languages')->where('lang_key', $request->id)->exists();

        dd($exists);


        return view("{$this->blade}.edit", ['data' => $data]);
    } catch (\Throwable $th) {
        return back()->withErrors([$th->getMessage()]);
    }
}

This return false for id doesn't exist in database but why does it pass the validation?

0 likes
2 replies
xalafnasirov's avatar

But it works in this way

        $validated = Validator::make([
            'id' => $id
        ], [
            'id' => 'exists:languages,lang_key'
        ]);

        if ($validated->fails()) {
            return back()->withErrors(['Key does not exist']);
        }

I need an explanation rather than solution for the problem above Thanks

Mega_Aleksandar's avatar

Hi,

Lets try and debug this. How is your request getting to the controller? Maybe you are passing some other unexpected id and it passes the first one and not the second. Also, double check the connection (as per docs here https://laravel.com/docs/11.x/validation#rule-exists) for the rule. And for the second part, how are you providing the $id to the Validator?

Please or to participate in this conversation.