How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: Laravel Nullable Exists Validation (nullable, sometimes, present)
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > Laravel Nullable Exists Validation (nullable, sometimes, present)
Web Development

Laravel Nullable Exists Validation (nullable, sometimes, present)

how7o
By how7o
Last updated: May 3, 2026
9 Min Read
Laravel nullable exists validation — nullable, sometimes, and present rule combinations
SHARE

Laravel nullable exists validation is how you validate a foreign-key field that is optional — the value must exist in another table when provided, but null and missing values should still pass. The one-liner is 'nullable|exists:table,column', but there are three rule combinations (nullable, sometimes, present) that behave differently depending on whether the key is absent from the payload, empty-string, or null. This guide covers each, when to reach for which, and the empty-string trap that catches most developers.

Contents
  • TL;DR
  • The canonical fix: nullable|exists
  • When nullable isn’t enough: sometimes
  • Strict APIs: present|nullable|exists
  • The empty-string trap
  • Quick reference: which rule when
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on Laravel 11 with PHP 8.3. Originally published 2023-01-30 & 2023-10-10, merged, rewritten, and updated 2026-04-23.

TL;DR

Use 'vehicle_model_id' => 'nullable|exists:vehicle_models,id'. The nullable rule tells Laravel to treat null as valid; if a non-null value is sent, exists checks the database. Add sometimes in front if the key itself may be missing from the payload.

The canonical fix: nullable|exists

The most common shape of this problem: a form has an optional foreign-key field — a vehicle_model_id on a vehicle record, a category_id on a product, a parent_id on a comment. The column is nullable in the database, and the request payload may send null (or omit the key). Without nullable, Laravel’s exists rule will run against null and fail with “The selected vehicle_model_id is invalid.”

The fix is a single extra rule:

$request->validate([
    'vehicle_model_id' => 'nullable|exists:vehicle_models,id',
]);

Rule order doesn’t affect behavior here — Laravel evaluates all rules — but the convention is to put nullable first because it’s the “gate”: if the value is null, the validator short-circuits to success and never hits the database. That matters on high-traffic endpoints where skipping an unnecessary query per optional field adds up.

When nullable isn’t enough: sometimes

nullable only whitelists the literal PHP null. If the request doesn’t include the key at all — a partial-update API where the client omits unchanged fields — Laravel still treats absence differently from null. For that case, reach for sometimes:

$request->validate([
    'vehicle_model_id' => 'sometimes|exists:vehicle_models,id',
]);

sometimes means “only run subsequent rules if this field is present in the request.” If the client sends {"make": "Toyota"} without a vehicle_model_id key, the validator skips the exists check entirely. If the client sends {"vehicle_model_id": null}, sometimes sees it as present and the exists rule runs against null — which fails. For the “optional and nullable” case, stack both:

$request->validate([
    'vehicle_model_id' => 'sometimes|nullable|exists:vehicle_models,id',
]);
laravel nullable exists validation — sometimes vs nullable vs present decision flow

Strict APIs: present|nullable|exists

The opposite of sometimes is present. present requires the key to exist in the payload but places no requirement on its value. Pair it with nullable and exists to say “the field must be sent — but its value can be null or a valid foreign key”:

$request->validate([
    'vehicle_model_id' => 'present|nullable|exists:vehicle_models,id',
]);

This is useful for strict API contracts where a missing key is a client bug — typically when the frontend is supposed to clear the value to null explicitly rather than omit it. For loose contracts (partial updates, optional UI fields), stick with sometimes.

The empty-string trap

HTML forms send empty <input> values as the empty string "", not null. nullable does not whitelist empty strings — only the literal PHP null. So this still fails:

// Request payload: { "vehicle_model_id": "" }
$request->validate([
    'vehicle_model_id' => 'nullable|exists:vehicle_models,id',
]);
// "The selected vehicle_model_id is invalid."

Laravel ships with a middleware that fixes this for standard form requests: Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull is registered in the web and api middleware groups by default. It rewrites empty strings to null before your controller ever sees the payload, and nullable then handles them correctly. If you’ve removed that middleware (or you’re receiving data through a channel that bypasses it — queue jobs, console commands, direct model creation), you have two options:

  1. Re-register ConvertEmptyStringsToNull for the relevant middleware group in bootstrap/app.php.
  2. Add filled alongside nullable: 'nullable|filled|exists:...' — filled skips the rule when the field is present but empty.

Quick reference: which rule when

Payloadnullablesometimespresent
Key missingfailsskipsfails
Value is nullpassesruns exists (fails)runs exists (fails)
Value is ""runs exists (fails)runs exists (fails)runs exists (fails)
Value is valid FKpassespassespasses

Read down each column: pick nullable when the client always sends the key but the value may be null, sometimes when the key itself may be absent, present when the key is mandatory but a null value is acceptable.

Frequently asked questions

What’s the shortest laravel nullable exists validation rule?

'field' => 'nullable|exists:table,column'. Laravel’s nullable rule tells the validator to treat null and missing values as valid; if a value is present, the exists rule then runs against the database. This is the one-line fix that covers the typical “foreign key that might be null” case.

When should I use sometimes instead of nullable?

Use sometimes when the field might be absent from the request entirely (not sent as a key at all). nullable only whitelists null; if the request never includes the key, nullable on its own still requires you to also allow that. sometimes|exists:table,column means “only run validation if the key is present.” Stack them for the safest variant: sometimes|nullable|exists:table,column.

Does nullable allow empty strings?

No. nullable only whitelists the literal PHP null. An empty string "" is still treated as a present value, so exists would run and fail. If your frontend sends empty strings, either coerce them to null in middleware (ConvertEmptyStringsToNull in app/Http/Kernel.php is Laravel’s default) or add the rule 'nullable|filled|exists:...' so the validator skips truly empty input.

What does present|exists|nullable do?

present requires the key to exist in the request payload but allows its value to be anything, including null. Combined with exists and nullable, it means “the field must be sent, but its value can be null or a valid foreign key.” Useful for strict API contracts where a missing key is a client bug even when the value is optional.

Why does my exists rule still fail on a nullable field?

Two common causes. First, the value being sent is an empty string, not null — see the previous answer. Second, the order of rules matters when you read errors but not when validation runs; Laravel evaluates all rules, so nullable always short-circuits for null regardless of position. If it’s still failing, dd($request->all()) and check the actual payload type.

Related guides

  • How to Check if Exists in the Database with the Laravel Validator — the base exists rule, unique counterpart, and custom table/column targeting.
  • How to Check if a Record Exists in Laravel — querying existence in Eloquent (outside the validator).
  • How to Add Foreign Keys in Laravel Migration — define the schema that exists validates against.
  • How to Install Laravel on Ubuntu — set up Laravel 11 and Artisan before validating anything.

References

Official Laravel validation docs (nullable, sometimes, present, exists, filled): laravel.com/docs/validation.

TAGGED:LaravelphpValidation

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Fake Windows update prank — fullscreen blue Windows update screen on an office laptop How to Prank a Coworker With a Fake Windows Update
Next Article Laravel validate in rule — restricting input to an allow-list with in: and Rule::in Laravel Validate Input to Specific Values (in Rule)
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Laravel get config variable — config() helper and Config facade resolving dotted keys
How to Get Config Variables in Laravel
May 3, 2026
Laravel call controller from another controller — app() and constructor injection patterns
How to Call a Controller Method from Another Controller in Laravel
May 3, 2026
Laravel Carbon not found error — namespace import fix
Fix “Class Carbon not found” in Laravel
May 3, 2026
Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping
How to Fix “Unknown column ‘CONCAT'” in Laravel
May 3, 2026
Laravel validate in rule — restricting input to an allow-list with in: and Rule::in
Laravel Validate Input to Specific Values (in Rule)
May 3, 2026

You Might Also Like

CSS page break for printing shown in a print preview layout
Web Development

CSS Page Break for Printing: How to Split a Web Page Into Multiple Printed Pages

6 Min Read
Create custom exception class in Laravel (Artisan command + secure error handling)
Web Development

How to Create a Custom Exception Class in Laravel (With Clean JSON Responses)

6 Min Read
Deleting a Laravel user cascades to remove related posts, photos, and notifications
Web Development

How to Delete Related Records in Laravel Eloquent

6 Min Read
Laravel validator exists rule checking a post_id against the posts table
Web Development

How to Use the Laravel Validator Exists Rule

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?