Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function store(Request $request)
- {
- $this->validate($request, [
- 'title' => 'required',
- 'body' => 'required',
- 'post_image' => 'image|mimes:jpeg,png,jpg,gif,svg|nullable|max:2048'
- ]);
- if($request->hasFile('post_image')){
- $filenameWithExt = $request->file('post_image')->getClientOriginalName();
- $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
- $extFile = $request->file('post_image')->getClientOriginalExtension();
- $fileNameToStore = $filename.'_'.time().'.'.$extFile;
- $path = $request->file('post_image')->storeAs('public/images/', $fileNameToStore);
- }else{
- $fileNameToStore = 'noimg.jpg';
- }
- $post = new Post;
- $post->Title = $request->input('title');
- $post->Body = $request->input('body');
- $post->user_id = auth()->user()->id;
- $post->image = $fileNameToStore;
- $post->save();
- return redirect('/posts')->with('success', 'Post Created');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement