php
49 lines · 7 steps
How a custom coupon rule validates in Laravel
A Laravel validation rule checks a coupon code against existence, status, expiry, limits, and per-user reuse.
Explained by
highlit
1<?php
2
3namespace App\Rules;
4
5use App\Models\Coupon;
6use Closure;
7use Illuminate\Contracts\Validation\ValidationRule;
8
9class Redeemable implements ValidationRule
10{
11 public function __construct(protected ?int $userId = null)
12 {
13 }
14
15 public function validate(string $attribute, mixed $value, Closure $fail): void
16 {
17 $coupon = Coupon::whereRaw('LOWER(code) = ?', [strtolower($value)])->first();
18
19 if (! $coupon) {
20 $fail('The :attribute is not a valid coupon.')->translate();
21
22 return;
23 }
24
25 if (! $coupon->is_active) {
26 $fail('This coupon is no longer active.');
27
28 return;
29 }
30
31 if ($coupon->expires_at !== null && $coupon->expires_at->isPast()) {
32 $fail('This coupon expired on :date.')->translate([
33 'date' => $coupon->expires_at->toFormattedDateString(),
34 ]);
35
36 return;
37 }
38
39 if ($coupon->max_redemptions !== null && $coupon->redemptions_count >= $coupon->max_redemptions) {
40 $fail('This coupon has reached its redemption limit.');
41
42 return;
43 }
44
45 if ($this->userId !== null && $coupon->redemptions()->where('user_id', $this->userId)->exists()) {
46 $fail('You have already redeemed this coupon.');
47 }
48 }
49}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Custom rule objects let you encapsulate multi-step business logic behind Laravel's validation pipeline.
- 2Sequential guard clauses that each fail-and-return keep complex validation readable and short-circuited.
- 3Constructor parameters let a rule carry context, like the current user, into its checks.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-custom-coupon-rule-validates-in-laravel-explained-php-0527/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.