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

DerAndi's avatar

Validation and database query redundancy

Hey folks,

I have a question to improve my code and the performance of my laravel apps. I was wondering if there is any query cache or something like that when querying the same ressource multiple times.

Let's say I have the following validation rule:

'group' => 'exists:groups,id|required',

The validator will query the group with id X. After the validation I need the group entity to modify it or build a json respone with it. So I have to query it again within the controller.

I'm doing this all the time and I don't like it. Is this the common way? Or is there a better method to do this?

Thanks and greetings

Andreas

0 likes
3 replies
bobbybouwmann's avatar

Laravel doesn't offer this out of the box at the moment.

You can build in your own caching for queries, however you can't use that cache in the current exists validation rule. What you can do is setup your own validation rule that uses this caching part. This way you can reuse the same query (or it's cache) during the same request.

1 like
DerAndi's avatar

Alright, thanks for your answer! :)

clem's avatar

I'm also not a big fan of the duplicate queries. Is there still no built-in solution?

At the moment I do the exists validation outside to keep the result.

Please or to participate in this conversation.