Skip to content

Commit f127f61

Browse files
committed
Practice on the form request class in controller and API both
1 parent 67e2355 commit f127f61

File tree

9 files changed

+253
-5
lines changed

9 files changed

+253
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\StorePostRequest;
7+
use App\Models\Post;
8+
use Illuminate\Http\JsonResponse;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Validator;
11+
12+
class ManagePostApiController extends Controller
13+
{
14+
public function index()
15+
{
16+
$posts = Post::orderBy('id', 'DESC')->get();
17+
18+
return response()->json([
19+
'success' => true,
20+
'data' => $posts
21+
], 200);
22+
}
23+
24+
public function store(Request $request)
25+
{
26+
$validator = Validator::make($request->all(), [
27+
'title' => 'required',
28+
'description' => 'required',
29+
'images' => 'required',
30+
'status' => 'required'
31+
]);
32+
33+
if ($validator->fails()) {
34+
return response()->json([
35+
'status' => false,
36+
'message' => $validator
37+
]);
38+
}
39+
40+
$validated = $validator->validated();
41+
42+
$validated = $validator->safe()->except(['title', 'title']);
43+
$validated = $validator->safe()->except(['description', 'description']);
44+
$validated = $validator->safe()->except(['images', 'images']);
45+
$validated = $validator->safe()->except(['status', 'status']);
46+
47+
$input = $request->all();
48+
// dd($postimg = $request->file('images'));
49+
50+
if ($postimg = $request->file('images')) {
51+
$imagepath = public_path('storage/post');
52+
$postname = date('YmdHis') . '.' . $postimg->getClientOriginalExtension();
53+
$postimg->move($imagepath, $postname);
54+
$input['images'] = 'storage/post/' . $postname;
55+
}
56+
57+
// dd($input['images']);
58+
59+
Post::create([
60+
'title' => $input['title'],
61+
'description' => $input['description'],
62+
'images' => $input['images'],
63+
'status' => $input['status']
64+
]);
65+
66+
// dd($input);
67+
68+
return response()->json([
69+
'status' => true,
70+
'message' => 'Post created Successfully!',
71+
'data' => $input
72+
]);
73+
}
74+
75+
public function update($id, StorePostRequest $request): JsonResponse
76+
{
77+
78+
// dd($request->hasFile('images')); //false
79+
$validated = $request->validated();
80+
81+
$validated = $request->safe()->except(['title', 'description', 'images', 'status']);
82+
83+
$updateInput = $request->all();
84+
// dd($request->file('images'));
85+
86+
if ($updatePostImg = $request->file('images')) {
87+
$updatedPath = public_path('storage/post/updatedimg');
88+
$postName = 'updateimg' . date('YmdHis') . '.' . $updatePostImg->getClientOriginalExtension();
89+
$updatePostImg->move($updatedPath, $postName);
90+
$updateInput['images'] = 'storage/post/updatedimg/' . $postName;
91+
}
92+
// dd($updatePostImg = $request->file('images'));
93+
94+
$updatePost = Post::findOrFail($id);
95+
// dd($updatePost);
96+
$updatePost->update($updateInput);
97+
// dd($updatePost->update($updateInput));
98+
99+
return response()->json([
100+
'success' => true,
101+
'message' => 'Post Updated Successfully!',
102+
'data' => $updatePost,
103+
], 200);
104+
}
105+
106+
public function show($id)
107+
{
108+
$findpost = Post::find($id);
109+
if(!$findpost)
110+
{
111+
return response()->json([
112+
'status' => false,
113+
'message' => 'Post Not Found'
114+
]);
115+
}
116+
117+
$findpost->first();
118+
119+
return response()->json([
120+
'status' => true,
121+
'message' => 'Post founded successfully!',
122+
'data' => $findpost
123+
]);
124+
}
125+
126+
public function delete($id)
127+
{
128+
$finddelete = Post::find($id);
129+
130+
if(!$finddelete)
131+
{
132+
return response()->json([
133+
'status' => false,
134+
'message' => 'Post Not found!'
135+
]);
136+
}
137+
138+
$finddelete->delete();
139+
140+
return response()->json([
141+
'status' => true,
142+
'message' => 'Post Deleted Successfully!'
143+
]);
144+
}
145+
}

app/Http/Controllers/PostController.php

+60
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,64 @@ public function edit($id)
100100
$posts = Post::find($id);
101101
return view('admin.blog.edit', compact('posts'));
102102
}
103+
104+
public function update($id, StorePostRequest $request): RedirectResponse
105+
{
106+
$validated = $request->validated();
107+
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();
114+
115+
// dd($updatepostimg = $request->file('images'));
116+
117+
if($updatepostimg = $request->file('images'))
118+
{
119+
$updatedpath = public_path('storage/post/updatedimg');
120+
$postname = 'updateimg'.date('YmdHis').'.'.$updatepostimg->getClientOriginalExtension();
121+
$updatepostimg->move($updatedpath, $postname);
122+
$updateinput['images'] = 'storage/post/updatedimg/'.$postname;
123+
}
124+
125+
// dd($updateinput['images']);
126+
127+
$updatepost = Post::find($id);
128+
// dd($updatepost);
129+
// dd($updateinput);
130+
$updatepost->update($updateinput);
131+
// dd($updatepost);
132+
133+
return redirect()->route('blog.home')->with('success', 'Post Updated Successfully!');
134+
}
135+
136+
public function delete($id)
137+
{
138+
$deletepost = Post::find($id);
139+
140+
if(!$deletepost)
141+
{
142+
return redirect()->back()->with('error', 'Post not found');
143+
}
144+
145+
$deletepost->delete();
146+
147+
return redirect()->route('blog.home')->with('success', 'Post deleted successfully!');
148+
}
149+
150+
public function show($id)
151+
{
152+
$findpost = Post::find($id);
153+
154+
if(!$findpost)
155+
{
156+
return view('blog.home')->with('error', 'Post not found!');
157+
}
158+
159+
$findpost->first();
160+
161+
return view('admin.blog.show', compact('findpost'));
162+
}
103163
}

app/Http/Requests/StorePostRequest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Requests;
44

55
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Support\Arr;
67

78
class StorePostRequest extends FormRequest
89
{
@@ -31,7 +32,7 @@ public function rules(): array
3132
];
3233
}
3334

34-
public function messages()
35+
public function messages(): array
3536
{
3637
return [
3738
'title.required' => 'Title is required!',

resources/views/admin/blog/create.blade.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<div class="col-md-6">
3232
<div class="form-group">
3333
<strong>Title</strong>
34-
<input type="text" name="title" class="form-control" placeholder="Name">
34+
<input type="text" name="title" class="form-control" placeholder="Blog title here">
3535
</div>
3636
</div>
3737

@@ -45,11 +45,12 @@
4545
<div class="col-md-12">
4646
<div class="form-group">
4747
<strong>Description:</strong>
48-
<textarea class="form-control" style="height: 150px" name="description" placeholder="Description"></textarea>
48+
<textarea class="form-control" style="height: 150px" name="description" placeholder="Share your experiance in your words..."></textarea>
4949
</div>
5050
</div>
5151

5252
<div class="col-md-12">
53+
<strong>Status</strong>
5354
<div class="form-check">
5455
<input class="form-check-input" type="radio" name="status" id="status" value="Active">
5556
<label class="form-check-label" for="status">Active</label>

resources/views/admin/blog/edit.blade.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<form action="{{route('blog.update', $posts->id)}}" method="post" enctype="multipart/form-data">
2727

2828
@csrf
29+
@method('PUT')
2930

3031
<div class="row">
3132
<div class="col-md-6">

resources/views/admin/blog/index.blade.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@extends('layouts.admin')
22
@section('content')
3+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
4+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
35
<div class="row">
46
<div class="col-lg-12 margin-tb">
57
<div class="pull-left">
@@ -32,7 +34,15 @@
3234
<td class="text-center">{{ $post->title }}</td>
3335
<td>{{ Str::limit($post->description, 10) }}</td>
3436
<td><img src="{{$post->images}}" alt="{{$post->title}}" width="90px" height="50px"></td>
35-
<td class="text-center">{{$post->status}}</td>
37+
<td class="text-center">
38+
@if($post->status == 'Active')
39+
<span class="badge rounded-pill text-bg-success">{{$post->status}}</span>
40+
@elseif($post->status == 'Draft')
41+
<span class="badge rounded-pill text-bg-warning">{{$post->status}}</span>
42+
@else
43+
<span class="badge rounded-pill text-bg-danger">{{$post->status}}</span>
44+
@endif
45+
</td>
3646
<td>
3747
<form action="{{ route('blog.delete',$post->id) }}" method="POST">
3848
<a class="btn btn-info" href="{{ route('blog.show',$post->id) }}">Show</a>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@extends('layouts.admin')
2+
@section('content')
3+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
4+
5+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
6+
7+
<div class="card">
8+
<div class="card-header">
9+
<h2><strong>Detailed Post</strong></h2>
10+
<span>Post ID: {{$findpost->id}}</span>
11+
<a href="{{route('blog.home')}}" class="btn btn-primary float-end p-2">Back</a>
12+
</div>
13+
<div class="card-body">
14+
<div class="col-sm-4 float-end">
15+
<img src="/{{$findpost->images}}" alt="{{$findpost->title}}" width="200px">
16+
</div>
17+
<label class="text-danger">Post Title </label>
18+
<h4 class="mb-5">{{$findpost->title}}</h4>
19+
<label class="text-danger">Post Description </label>
20+
<p>{{$findpost->description}}</p>
21+
</div>
22+
</div>
23+
@endsection

resources/views/layouts/admin.blade.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<head>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link rel="stylesheet" href="{{ asset('css/styles.css') }}">
87
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/admin-lte/3.1.0/css/adminlte.min.css">
98
<title>Admin Panel</title>
109

routes/api.php

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Http\Controllers\API\Admin\ManagePostApiController;
34
use App\Http\Controllers\API\Admin\ManageProductAPIController;
45
use App\Http\Controllers\API\Admin\ManageRolesController;
56
use App\Http\Controllers\API\Admin\ManageUserController;
@@ -25,6 +26,13 @@
2526
// Route::apiResource("products", ProductController::class);
2627
Route::post('register',[AuthController::class,'register']);
2728
Route::post('login', [AuthController::class, 'login']);
29+
30+
Route::get('blogs',[ManagePostApiController::class, 'index']);
31+
Route::post('blogs/create', [ManagePostApiController::class, 'store']);
32+
Route::put('blogs/update/{id}', [ManagePostApiController::class, 'update']);
33+
Route::get('blogs/show/{id}', [ManagePostApiController::class, 'show']);
34+
Route::delete('blogs/delete/{id}', [ManagePostApiController::class, 'delete']);
35+
2836
Route::middleware('auth:sanctum')->group(function(){
2937

3038
Route::group(['middleware' => ['role:admin']], function(){

0 commit comments

Comments
 (0)