Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mawuva/laravel-api-form-request
Extending FormRequest for REST API Request Validation
https://github.com/mawuva/laravel-api-form-request
Last synced: 7 days ago
JSON representation
Extending FormRequest for REST API Request Validation
- Host: GitHub
- URL: https://github.com/mawuva/laravel-api-form-request
- Owner: mawuva
- License: mit
- Created: 2021-06-12T18:44:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-24T21:31:33.000Z (over 3 years ago)
- Last Synced: 2024-04-01T18:22:39.769Z (8 months ago)
- Language: PHP
- Homepage: https://packagist.org/packages/mawuekom/laravel-api-form-request
- Size: 34.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# API Form Request
Extending FormRequest for REST API Request Validation
## Installation
You can install the package via composer:
```bash
composer require mawuekom/laravel-api-form-request
```## Usage
```php
namespace App\Http\Requests;use Mawwuekom\ApiFormRequest\ApiFormRequest;
class CreateUserRequest extends ApiFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|max:255',
'first_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
];
}
}
```Once done, it will look like this in your controller
```php
namespace App\Http\Controllers;use App\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateUserRequest;class UserController extends Controller
{public function register(CreateUserRequest $request)
{
User::create([
'name' => request('name'),
'first_name' => request('first_name'),
'email' => request('email'),
'password' => bcrypt(request('password')),
]);
}
}
```
And the errors will look like this```json
{
"errors": [
{
"field": "email",
"message": "The email has already been taken."
},
{
"field": "password",
"message": "The password confirmation does not match."
}
]
}
```Hope this package will help you to build great things... 🏙️ Have fun 👍
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.