Access to undeclared static property: User::$rules in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Access to Undeclared Static Property: Solving Validation Errors in Laravel Models
As developers working with the Laravel ecosystem, we frequently encounter subtle but frustrating errors, especially when dealing with Eloquent models and validation. The error you are seeing—Access to undeclared static property: User::$rules—is a classic example of how PHP's strict typing and object-oriented principles interact with framework conventions.
This post will dive deep into why this error occurs in the context of Laravel validation, explore the pitfalls of using static properties for rules, and demonstrate the best, most idiomatic ways to handle form validation in modern Laravel applications.
Understanding the Error: Static Properties Misuse
The core issue lies in how you are attempting to access $rules on your User model within your controller logic:
$validation = Validator::make($input, User::$rules); // Error occurs here
In PHP, when you try to access a property using the scope resolution operator (::), you are asking for a static property of that class. The error message clearly indicates that the User class does not possess a publicly declared static property named $rules.
Why This Happens in Laravel
When defining model properties like $table or $hidden, you define them as standard public or protected properties. However, validation rules—which are highly contextual and often dynamic based on the request—are typically managed outside the core Eloquent model definition for better separation of concerns.
Attempting to store validation rules directly onto the Eloquent model using a static property like this is generally considered an anti-pattern in large applications because:
- Tight Coupling: It tightly couples your business logic (validation rules) directly into the data model.
- Maintainability: If you need different sets of rules for different aspects of the user (e.g., registration vs. updating), managing these static properties becomes cumbersome and error-prone.
The Idiomatic Laravel Solution: Form Requests
The most robust and recommended way to handle validation in Laravel is by utilizing Form Request Classes. This approach separates the concerns of input handling (the controller's job) from validation logic (a dedicated class's job). This aligns perfectly with the principles of clean, testable code that Laravel champions.
Instead of baking rules into the model, you create a separate request class to define exactly what data is expected for a specific operation.
Step 1: Define the Form Request
Create a new Form Request class (e.g., UpdateUserRequest.php):
// app/Http/Requests/UpdateUserRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true; // Or implement proper authorization checks
}
/**
* Get the validation rules that apply to the request.
* This replaces the need for static properties on the model.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
// Define all validation rules here explicitly
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users,email'],
'password' => ['sometimes', 'string', 'min:8'],
];
}
}
Step 2: Use the Form Request in the Controller
Now, your controller becomes much cleaner. You simply inject the Form Request into your method, and Laravel handles all the validation automatically before the code execution proceeds.
// In your UserController.php
use App\Http\Requests\UpdateUserRequest;
use App\Models\User; // Assuming you use Eloquent models
public function update($id, UpdateUserRequest $request)
{
$user = User::findOrFail($id);
// Laravel automatically validates the request data against the rules defined in UpdateUserRequest
$user->update($request->validated());
return redirect()->route('users.show', $id);
}
Conclusion
The error Access to undeclared static property: User::$rules is a direct indicator that you should pivot away from embedding complex validation logic directly into Eloquent model static properties. By adopting the Form Request pattern, you achieve superior code separation, enhance testability, and adhere to Laravel's philosophy of building elegant, maintainable applications. Always strive to keep your models focused on data structure (like $table or $hidden), letting dedicated classes handle complex business rules like validation. For more insights into structuring robust APIs in the Laravel world, exploring resources from the Laravel Company is highly recommended.