shureban/laravel-easy-request
Laravel package that adds typed getters to FormRequest via PHPDoc @method annotations. Call $request->name(), $request->age(), etc., and values are cast to bool/int/float/Carbon/DateTime. Supports camelCase methods with snake_case input keys.
Installation:
composer require shureban/laravel-easy-request
Register the service provider in config/app.php:
Shureban\LaravelEasyRequest\EasyRequestServiceProvider::class,
Publish Config (Optional):
php artisan vendor:publish --provider="Shureban\LaravelEasyRequest\EasyRequestServiceProvider"
First Use Case:
Create a FormRequest class with PhpDoc annotations for type-safe access to request data:
/**
* @method string getName()
* @method int getAge()
*/
class UserProfileRequest extends FormRequest {}
Use it in a controller:
public function update(UserProfileRequest $request) {
$name = $request->getName(); // Returns string (type-safe)
$age = $request->getAge(); // Returns int (type-safe)
}
Type-Safe Request Handling:
int, bool, DateTime).userId() fetches user_id from input).Model Instantiation:
@method User user()) to auto-fetch Eloquent models by ID:
/**
* @method User user()
*/
class UserRequest extends FormRequest {
public function rules() { return ['user_id' => 'required|int']; }
}
Integration with Laravel Ecosystem:
FormRequest::rules() for validation.FormRequest::authorize() for policy checks.JsonResponse for consistent JSON output.Dynamic Requests:
EasyRequest) for non-form requests:
$response = EasyRequest::get('https://api.example.com/data')
->withHeaders(['Authorization' => 'Bearer token'])
->send();
Testing:
FormRequest methods in tests:
$request = Mockery::mock(UserRequest::class);
$request->shouldReceive('getName')->andReturn('John');
Type Casting Edge Cases:
null → int fails). Handle with @method mixed|null or default values:
/**
* @method int|null age()
*/
Model Fetching Quirks:
_id suffix or Id in camelCase (e.g., user_id → user() or userId()).->firstOrFail() or validation).Facade vs. FormRequest:
EasyRequest facade is for dynamic HTTP calls; FormRequest is for typed form data. Mixing them may cause confusion.Performance:
php artisan ide-helper:generate (if using Barryvdh/laravel-ide-helper) to validate PhpDoc syntax.$request->all() to verify field names.'guzzle' => [
'debug' => env('APP_DEBUG', false),
],
Custom Type Casting:
Override the default caster by publishing the config and extending Shureban\LaravelEasyRequest\Caster\CasterInterface.
Model Resolution:
Extend Shureban\LaravelEasyRequest\ModelResolver to support custom ID fields or relationships.
Request Macros: Add reusable methods to the facade:
EasyRequest::macro('withAuth', function ($token) {
return $this->withHeaders(['Authorization' => "Bearer $token"]);
});
Testing Utilities: Create a trait for testable requests:
trait TestableRequest {
protected function mockRequest(array $data) {
return Mockery::mock(UserRequest::class)->makePartial()
->shouldReceive('all')->andReturn($data);
}
}
How can I help you explore Laravel packages today?