How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Use the Laravel Validator Exists Rule
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Use the Laravel Validator Exists Rule
Web Development

How to Use the Laravel Validator Exists Rule

how7o
By how7o
Last updated: April 19, 2026
6 Min Read
Laravel validator exists rule checking a post_id against the posts table
SHARE

When you’re validating a form that references an existing row — editing a post, attaching a tag, updating a user — you can skip writing a manual existence check in the controller. Laravel’s validator has a built-in exists rule that runs the database check for you and fails validation automatically if the reference is invalid.

Contents
  • TL;DR
  • The Manual Check You’re Replacing
  • Using the exists Rule
    • Table Form
    • Model Form
  • Adding Constraints to the Rule
  • The Opposite: unique Rule
  • Troubleshooting
    • “Table Does Not Exist” From the Validator
    • Validation Passes But the Record Is Soft-Deleted
    • SQL Injection From User Input in Rule::exists()
  • Frequently Asked Questions
  • Related Guides

Originally published August 30, 2022, rewritten and updated April 17, 2026.

TL;DR

Add exists:table,column (or exists:App\Models\ModelName,column) to any validation rule array:

'post_id' => 'required|exists:posts,id'

If no row with id = $request->post_id exists in posts, validation fails before your controller body runs.

The Manual Check You’re Replacing

Without the rule, you end up splitting validation across two places — the validator handles required/format rules, and the controller reaches into the database to confirm the ID is real:

$post = Post::where('id', $request['post_id']);
if ($post->exists()) {
    // Update the post
}

Moving the existence check into the validator centralizes the logic, returns a proper 422 validation response on failure, and keeps the controller focused on the happy path.

Using the exists Rule

Table Form

The simplest form takes a table name and a column:

$this->validate($request, [
    'post_id'      => 'required|exists:posts,id',
    'post_title'   => 'required|string|max:255',
    'post_content' => 'required',
]);

This asks the database: “Does a row exist in posts where id equals the submitted post_id?” If not, the validator adds an error for that field.

Model Form

You can also pass a model class instead of a table name. Laravel resolves the table from the model, which means model-level customizations like a non-default table name or a global scope can apply:

'post_id' => 'required|exists:App\Models\Post,id'

Adding Constraints to the Rule

If you need the row to match more than just the primary key — say, you want to allow only published posts owned by the current user — use the Rule::exists() fluent builder:

use Illuminate\Validation\Rule;

$this->validate($request, [
    'post_id' => [
        'required',
        Rule::exists('posts', 'id')
            ->where('user_id', auth()->id())
            ->where('status', 'published'),
    ],
]);

Each ->where(...) adds a constraint to the underlying query. This is the cleanest way to validate “the row exists AND belongs to the current user” in one pass, without leaking authorization into the controller.

Laravel exists validation rule checking a post_id against the posts table with a scoped query

The Opposite: unique Rule

The inverse of exists is unique. Use it to make sure a value is not already taken — typically on registration forms:

'email' => 'required|email|unique:users,email'

When updating an existing user, you usually want to exempt their own row from the uniqueness check, which you can do with Rule::unique()->ignore():

'email' => [
    'required',
    'email',
    Rule::unique('users', 'email')->ignore($user->id),
],

Troubleshooting

“Table Does Not Exist” From the Validator

If the exists rule reports a missing table, double-check the table name (not the model name) in the string form. When passing a model, use the fully-qualified class name (e.g. App\Models\Post, not just Post).

Validation Passes But the Record Is Soft-Deleted

The exists rule runs a raw query against the table, so it matches soft-deleted rows too. If you want to exclude soft-deleted records, add an explicit constraint:

Rule::exists('posts', 'id')->whereNull('deleted_at')

SQL Injection From User Input in Rule::exists()

Values passed to ->where() are bound as parameters, so user input in those clauses is safe. However, never interpolate user input directly into the table name or column name — those are not bound. Always use fixed strings for the table and column arguments.

Frequently Asked Questions

What’s the syntax for the Laravel exists validation rule?

<code>exists:table,column</code> — for example, <code>’post_id’ => ‘required|exists:posts,id'</code>. You can also pass a fully-qualified model class: <code>exists:App\Models\Post,id</code>.

How do I add extra conditions to the exists rule?

Use the fluent <code>Rule::exists()</code> builder and chain <code>->where()</code> calls. For example, <code>Rule::exists(‘posts’, ‘id’)->where(‘user_id’, auth()->id())</code> validates both that the post exists and that it belongs to the current user.

Does the exists rule include soft-deleted rows?

Yes. The rule runs a raw query against the table and does not know about soft-delete scopes. To exclude soft-deleted rows, add <code>->whereNull(‘deleted_at’)</code> to the Rule builder.

What’s the opposite of the exists rule?

<code>unique</code>. Use it when a value must not already be in the database — for example, a unique email on registration. When updating, use <code>Rule::unique(‘users’, ’email’)->ignore($user->id)</code> to allow the user’s own current value.

Related Guides

  • How to Check if a Record Exists in Laravel
  • Laravel updateOrCreate: Insert or Update Records in Eloquent
  • How to Install Laravel

For the complete list of built-in rules, see the official Laravel validation documentation.

TAGGED:EloquentLaravelphpValidation

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
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 Laravel Eloquent exists method checking if a record exists in a database query How to Check if a Record Exists in Laravel
Next Article Laravel foreign key constraint linking posts.user_id to users.id in a schema diagram How to Add Foreign Keys in Laravel Migration
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

Laravel rollback specific migration — Artisan migrate:rollback --path command illustration
Web Development

How to Rollback a Specific Migration in Laravel

8 Min Read
WordPress custom avatar without a plugin — media uploader writes user meta, get_avatar filter renders the image
Web Development

How to Change a User Profile Picture in WordPress Without a Plugin

10 Min Read
Set and get cookies with JavaScript
Web Development

How to Set and Get Cookies with JavaScript

6 Min Read
Login to Laravel programmatically without a password (Auth::login and loginUsingId)
Web Development

Login to Laravel Programmatically Without a Password (Auth::login & loginUsingId)

4 Min Read
How7o

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

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?