Skip to content

Commit 843b109

Browse files
committed
Changes in form request
1 parent f127f61 commit 843b109

File tree

12 files changed

+361
-36
lines changed

12 files changed

+361
-36
lines changed

app/Http/Controllers/API/Admin/ManagePostApiController.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Http\Requests\StorePostRequest;
7+
use App\Http\Requests\UpdatePostRequest;
78
use App\Models\Post;
89
use Illuminate\Http\JsonResponse;
910
use Illuminate\Http\Request;
@@ -72,13 +73,26 @@ public function store(Request $request)
7273
]);
7374
}
7475

75-
public function update($id, StorePostRequest $request): JsonResponse
76+
public function update($id, UpdatePostRequest $request): JsonResponse
7677
{
78+
dd($request->all());
79+
$validator = Validator::make($request->all(), [
80+
'title' => 'required',
81+
'description' => 'required',
82+
'images' => 'required',
83+
'status' => 'required'
84+
]);
7785

78-
// dd($request->hasFile('images')); //false
79-
$validated = $request->validated();
86+
if ($validator->fails()) {
87+
return response()->json([
88+
'status' => false,
89+
'message' => $validator
90+
]);
91+
}
92+
//false
93+
// $validated = $request->validated();
8094

81-
$validated = $request->safe()->except(['title', 'description', 'images', 'status']);
95+
// $validated = $request->safe()->except(['title', 'description', 'images', 'status']);
8296

8397
$updateInput = $request->all();
8498
// dd($request->file('images'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class LanguageController extends Controller
8+
{
9+
//
10+
}

app/Http/Controllers/PostController.php

+20-30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Http\Requests\StorePostRequest;
6+
use App\Http\Requests\UpdatePostRequest;
67
use App\Models\Post;
78
use Illuminate\Http\RedirectResponse;
89
use Illuminate\Http\Request;
@@ -65,20 +66,19 @@ public function store(StorePostRequest $request): RedirectResponse
6566
{
6667
$validated = $request->validated();
6768

68-
$validated = $request->safe()->except(['title','title']);
69-
$validated = $request->safe()->except(['description','description']);
70-
$validated = $request->safe()->except(['images','images']);
71-
$validated = $request->safe()->except(['status','status']);
69+
$validated = $request->safe()->except(['title', 'title']);
70+
$validated = $request->safe()->except(['description', 'description']);
71+
$validated = $request->safe()->except(['images', 'images']);
72+
$validated = $request->safe()->except(['status', 'status']);
7273

7374
$input = $request->all();
7475
// dd($postimg = $request->file('images'));
7576

76-
if($postimg = $request->file('images'))
77-
{
77+
if ($postimg = $request->file('images')) {
7878
$imagepath = public_path('storage/post');
79-
$postname = date('YmdHis').'.'.$postimg->getClientOriginalExtension();
79+
$postname = date('YmdHis') . '.' . $postimg->getClientOriginalExtension();
8080
$postimg->move($imagepath, $postname);
81-
$input['images'] = 'storage/post/'.$postname;
81+
$input['images'] = 'storage/post/' . $postname;
8282
}
8383

8484
// dd($input['images']);
@@ -101,44 +101,35 @@ public function edit($id)
101101
return view('admin.blog.edit', compact('posts'));
102102
}
103103

104-
public function update($id, StorePostRequest $request): RedirectResponse
104+
public function update($id, UpdatePostRequest $request): RedirectResponse
105105
{
106106
$validated = $request->validated();
107107

108-
$validated = $request->safe()->except(['title','title']);
109-
$validated = $request->safe()->except(['description', 'description']);
110-
$validated = $request->safe()->except(['images','images']);
111-
$validated = $request->safe()->except(['status','status']);
112-
113-
$updateinput = $request->all();
108+
$updateinput = $request->except(['images']);
114109

115-
// dd($updatepostimg = $request->file('images'));
110+
$updatepost = Post::find($id);
116111

117-
if($updatepostimg = $request->file('images'))
118-
{
112+
if ($request->hasFile('images')) {
113+
$updatepostimg = $request->file('images');
119114
$updatedpath = public_path('storage/post/updatedimg');
120-
$postname = 'updateimg'.date('YmdHis').'.'.$updatepostimg->getClientOriginalExtension();
115+
$postname = 'updateimg' . date('YmdHis') . '.' . $updatepostimg->getClientOriginalExtension();
121116
$updatepostimg->move($updatedpath, $postname);
122-
$updateinput['images'] = 'storage/post/updatedimg/'.$postname;
117+
$updateinput['images'] = 'storage/post/updatedimg/' . $postname;
118+
} else {
119+
$updateinput['images'] = $updatepost->images;
123120
}
124121

125-
// dd($updateinput['images']);
126-
127-
$updatepost = Post::find($id);
128-
// dd($updatepost);
129-
// dd($updateinput);
130122
$updatepost->update($updateinput);
131-
// dd($updatepost);
132123

133124
return redirect()->route('blog.home')->with('success', 'Post Updated Successfully!');
134125
}
135126

127+
136128
public function delete($id)
137129
{
138130
$deletepost = Post::find($id);
139131

140-
if(!$deletepost)
141-
{
132+
if (!$deletepost) {
142133
return redirect()->back()->with('error', 'Post not found');
143134
}
144135

@@ -151,8 +142,7 @@ public function show($id)
151142
{
152143
$findpost = Post::find($id);
153144

154-
if(!$findpost)
155-
{
145+
if (!$findpost) {
156146
return view('blog.home')->with('error', 'Post not found!');
157147
}
158148

app/Http/Requests/StorePostRequest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public function authorize(): bool
2222
*/
2323
public function rules(): array
2424
{
25+
2526
return [
2627
// Custome rule here:
2728

2829
'title' => 'required',
2930
'description' => 'required',
30-
'images' => 'required',
31+
'images' => 'required|nullable',
3132
'status' => 'required'
3233
];
3334
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UpdatePostRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'title' => 'nullable',
26+
'description' => 'nullable',
27+
'images' => 'nullable',
28+
'status' => 'required'
29+
];
30+
}
31+
32+
public function messages(): array
33+
{
34+
return [
35+
'status' => 'status is required'
36+
];
37+
}
38+
}

lang/en/auth.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Authentication Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines are used during authentication for various
11+
| messages that we need to display to the user. You are free to modify
12+
| these language lines according to your application's requirements.
13+
|
14+
*/
15+
16+
'failed' => 'These credentials do not match our records.',
17+
'password' => 'The provided password is incorrect.',
18+
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
19+
20+
];

lang/en/pagination.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Pagination Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines are used by the paginator library to build
11+
| the simple pagination links. You are free to change them to anything
12+
| you want to customize your views to better match your application.
13+
|
14+
*/
15+
16+
'previous' => '&laquo; Previous',
17+
'next' => 'Next &raquo;',
18+
19+
];

lang/en/passwords.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Password Reset Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines are the default lines which match reasons
11+
| that are given by the password broker for a password update attempt
12+
| has failed, such as for an invalid token or invalid new password.
13+
|
14+
*/
15+
16+
'reset' => 'Your password has been reset.',
17+
'sent' => 'We have emailed your password reset link.',
18+
'throttled' => 'Please wait before retrying.',
19+
'token' => 'This password reset token is invalid.',
20+
'user' => "We can't find a user with that email address.",
21+
22+
];

0 commit comments

Comments
 (0)