Advertisement
Guest User

STOREEEEEEE

a guest
Nov 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. public function store(Request $request)
  2.     {
  3.         $this->validate($request, [
  4.             'title' => 'required',
  5.             'body' => 'required',
  6.             'post_image' => 'image|mimes:jpeg,png,jpg,gif,svg|nullable|max:2048'
  7.         ]);
  8.         if($request->hasFile('post_image')){
  9.             $filenameWithExt = $request->file('post_image')->getClientOriginalName();
  10.  
  11.             $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
  12.  
  13.             $extFile = $request->file('post_image')->getClientOriginalExtension();
  14.  
  15.             $fileNameToStore = $filename.'_'.time().'.'.$extFile;
  16.  
  17.             $path = $request->file('post_image')->storeAs('public/images/', $fileNameToStore);
  18.         }else{
  19.             $fileNameToStore = 'noimg.jpg';
  20.         }
  21.  
  22.        
  23.         $post = new Post;
  24.         $post->Title = $request->input('title');
  25.         $post->Body = $request->input('body');
  26.         $post->user_id = auth()->user()->id;
  27.         $post->image = $fileNameToStore;
  28.         $post->save();
  29.  
  30.         return redirect('/posts')->with('success', 'Post Created');
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement