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

murilo's avatar
Level 10

laravel request ensure that value is NULL not NULLABLE

hello ,I need to ensure in laravel that area.id MUST be NULL . so I neeed to do something like this -

return [

'area.*.id' =>  'required|null',
];

the proble is that the null does not exist . it will give this error - Method Illuminate\Validation\Validator::validateNull does not exist.

it has nullable, but I dont want nullable . because nullable CAN also has a value . I NEED to ensure that the value is NULL .

0 likes
6 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To ensure that a specific field in a Laravel request is NULL, you can use a custom validation rule. Laravel does not have a built-in null validation rule, but you can create a custom rule to achieve this.

Here's how you can do it:

  1. Create a Custom Validation Rule:

First, you need to create a custom validation rule. You can do this using the make:rule Artisan command:

php artisan make:rule NullValue

This will create a new rule class in the app/Rules directory. Open the newly created NullValue class and modify it as follows:

  1. Use the Custom Rule in Your Request Validation:

Next, you need to use this custom rule in your request validation. Assuming you are using a Form Request for validation, you can do it like this:

use App\Rules\NullValue;

public function rules()
{
    return [
        'area.*.id' => ['required', new NullValue],
    ];
}

If you are not using a Form Request, you can apply the rule directly in your controller:

use App\Rules\NullValue;
use Illuminate\Http\Request;

public function store(Request $request)
{
    $request->validate([
        'area.*.id' => ['required', new NullValue],
    ]);

    // Your logic here
}

This custom rule will ensure that the area.*.id field must be NULL and will return an error message if it is not.

  1. Testing the Validation:

You can now test your validation by sending a request where area.*.id is NULL. If the value is not NULL, the validation will fail, and the custom error message will be displayed.

This approach ensures that the area.*.id field is strictly NULL and not just nullable.

tisuchi's avatar

@murilo I am not so sure I understand you correctly!

,I need to ensure in laravel that area.id MUST be NULL

That means to me, the value of id can be missing/null. In that case, doesn't nullable work for you?

If it doesn't you can always create custom validation rules where you can write whatever validation logic is required for you.

OussamaMater's avatar

If your logic requires a value to always be NULL, something is wrong with your code design.

1 like
Tray2's avatar

@murilo a value can not both be required and null at the same time, either it is required or it is null, take your pick. To store a null value in the database the column needs to be nullable.

However, I'm guessing that you are looking for it to be null in some cases and not null in some others, if so you can use required_if, or perhaps required_unless.

https://laravel.com/docs/11.x/validation#rule-required-if

https://laravel.com/docs/11.x/validation#rule-required-unless

Or as @tisuchi says, write your own custom rule.

1 like
JussiMannisto's avatar

And Lary's answer got the badge...

FWIW, the rules ['required', new NullValue] will always fail validation. Those two requirements are contradictory. Required will fail if the input is null, NullValue will fail if it's not.

Please or to participate in this conversation.