Skip to content

Commit 64011e2

Browse files
author
ahmadhuss
committed
refactor: Updated Validation rule for the POST Request
* Now we have created the `StoreFormRequest` for the categories create form.
1 parent 2867a01 commit 64011e2

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

app/Http/Controllers/Api/CategoryController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers\Api;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\StoreCategoryRequest;
67
use App\Http\Resources\CategoryResource;
78
use App\Models\Category;
89
use Illuminate\Http\Request;
@@ -32,9 +33,13 @@ public function show(Category $category){
3233
}
3334

3435
// Post store
35-
public function store(Request $request) {
36-
$category = Category::create($request->all());
37-
return new CategoryResource($category);
36+
public function store(StoreCategoryRequest $request) {
37+
try {
38+
$category = Category::create($request->all());
39+
return new CategoryResource($category);
40+
} catch (Illuminate\Database\QueryException $e) {
41+
42+
}
3843
}
3944

4045

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreCategoryRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'name' => 'required'
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)