app/Http/Controllers/PostController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $this->validate($request,[
            'title'=>'required',
            'body'=>'required',
            'cover_image' => 'image|nullable|max:1999'
        ]);
        if($request->hasFile('cover_image')){
            $filenameWithExt =$request->file('cover_image')->getClientOriginalName();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension =$request->file('cover_image')->getClientOriginalExtension();
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            $path= $request->file('cover_image')->storeAs('public/cover_images',$fileNameToStore);
        }else
        {
            $fileNameToStore = 'noimage.jpg';
        }
        $post=new Post;
        $post->title=$request->input('title');
        $post->body=$request->input('body');
        $post->user_id= auth()->user()->id;
        $post->cover_image =$fileNameToStore;
        $post->save();
        return redirect('/posts')->with('success', 'Post Created');
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'title' => 'required' , 'body' => 'required' , 'cover_image' => 'image|nullable|max:1999' ] ) ; if ( $ request -> hasFile ( 'cover_image' ) ) { $ filenameWithExt = $ request -> file ( 'cover_image' ) -> getClientOriginalName ( ) ; $ filename = pathinfo ( $ filenameWithExt , PATHINFO_FILENAME ) ; $ extension = $ request -> file ( 'cover_image' ) -> getClientOriginalExtension ( ) ; $ fileNameToStore = $ filename . '_' . time ( ) . '.' . $ extension ; $ path = $ request -> file ( 'cover_image' ) -> storeAs ( 'public/cover_images' , $ fileNameToStore ) ; } else { $ fileNameToStore = 'noimage.jpg' ; } $ post = new Post ; $ post -> title = $ request -> input ( 'title' ) ; $ post -> body = $ request -> input ( 'body' ) ; $ post -> user_id = auth ( ) -> user ( ) -> id ; $ post -> cover_image = $ fileNameToStore ; $ post -> save ( ) ; return redirect ( '/posts' ) -> with ( 'success' , 'Post Created' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//handle file upload //createpost