3
3
namespace App \Http \Controllers ;
4
4
5
5
use App \Http \Requests \StorePostRequest ;
6
+ use App \Http \Requests \UpdatePostRequest ;
6
7
use App \Models \Post ;
7
8
use Illuminate \Http \RedirectResponse ;
8
9
use Illuminate \Http \Request ;
@@ -65,20 +66,19 @@ public function store(StorePostRequest $request): RedirectResponse
65
66
{
66
67
$ validated = $ request ->validated ();
67
68
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 ' ]);
72
73
73
74
$ input = $ request ->all ();
74
75
// dd($postimg = $request->file('images'));
75
76
76
- if ($ postimg = $ request ->file ('images ' ))
77
- {
77
+ if ($ postimg = $ request ->file ('images ' )) {
78
78
$ imagepath = public_path ('storage/post ' );
79
- $ postname = date ('YmdHis ' ). '. ' . $ postimg ->getClientOriginalExtension ();
79
+ $ postname = date ('YmdHis ' ) . '. ' . $ postimg ->getClientOriginalExtension ();
80
80
$ postimg ->move ($ imagepath , $ postname );
81
- $ input ['images ' ] = 'storage/post/ ' . $ postname ;
81
+ $ input ['images ' ] = 'storage/post/ ' . $ postname ;
82
82
}
83
83
84
84
// dd($input['images']);
@@ -101,44 +101,35 @@ public function edit($id)
101
101
return view ('admin.blog.edit ' , compact ('posts ' ));
102
102
}
103
103
104
- public function update ($ id , StorePostRequest $ request ): RedirectResponse
104
+ public function update ($ id , UpdatePostRequest $ request ): RedirectResponse
105
105
{
106
106
$ validated = $ request ->validated ();
107
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 ();
108
+ $ updateinput = $ request ->except (['images ' ]);
114
109
115
- // dd($updatepostimg = $request->file('images') );
110
+ $ updatepost = Post:: find ( $ id );
116
111
117
- if ( $ updatepostimg = $ request ->file ('images ' ))
118
- {
112
+ if ( $ request ->hasFile ('images ' )) {
113
+ $ updatepostimg = $ request -> file ( ' images ' );
119
114
$ updatedpath = public_path ('storage/post/updatedimg ' );
120
- $ postname = 'updateimg ' . date ('YmdHis ' ). '. ' . $ updatepostimg ->getClientOriginalExtension ();
115
+ $ postname = 'updateimg ' . date ('YmdHis ' ) . '. ' . $ updatepostimg ->getClientOriginalExtension ();
121
116
$ 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 ;
123
120
}
124
121
125
- // dd($updateinput['images']);
126
-
127
- $ updatepost = Post::find ($ id );
128
- // dd($updatepost);
129
- // dd($updateinput);
130
122
$ updatepost ->update ($ updateinput );
131
- // dd($updatepost);
132
123
133
124
return redirect ()->route ('blog.home ' )->with ('success ' , 'Post Updated Successfully! ' );
134
125
}
135
126
127
+
136
128
public function delete ($ id )
137
129
{
138
130
$ deletepost = Post::find ($ id );
139
131
140
- if (!$ deletepost )
141
- {
132
+ if (!$ deletepost ) {
142
133
return redirect ()->back ()->with ('error ' , 'Post not found ' );
143
134
}
144
135
@@ -151,8 +142,7 @@ public function show($id)
151
142
{
152
143
$ findpost = Post::find ($ id );
153
144
154
- if (!$ findpost )
155
- {
145
+ if (!$ findpost ) {
156
146
return view ('blog.home ' )->with ('error ' , 'Post not found! ' );
157
147
}
158
148
0 commit comments