app/Http/Controllers/PostsController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'required',
            'body' => 'required'
        ]);
        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);
        }
       $post = Post::find($id);
       $post->title = $request->input('title');
       $post->body = $request->input('body');
       if($request->hasFile('cover_image')){
           $post->cover_image = $fileNameToStore;
       }
       $post->save();
       return redirect('/posts')->with('succes', 'Post Updated');
    }
public function update ( Request $ request , $ id ) { $ this -> validate ( $ request , [ 'title' => 'required' , 'body' => 'required' ] ) ; 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 ) ; } $ post = Post :: find ( $ id ) ; $ post -> title = $ request -> input ( 'title' ) ; $ post -> body = $ request -> input ( 'body' ) ; if ( $ request -> hasFile ( 'cover_image' ) ) { $ post -> cover_image = $ fileNameToStore ; } $ post -> save ( ) ; return redirect ( '/posts' ) -> with ( 'succes' , 'Post Updated' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
//Handle incarcarea de fisiere //Get filename with the extension //Get just filenamee //Get just extension //File name to store //Upload the Image //creez postare