app/Http/Controllers/PostsController.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', 
           'description' => 'required',
           'colour' => 'required',
           'found_location' => '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->type = $request->input('type');
       $post->cover_image = $fileNameToStore;
       $post->description = $request->input('description');
       $post->colour = $request->input('colour');
       $post->found_location = $request->input('found_location');
       $post->user_id =auth()->user()->id;
       $post->save();
       return redirect('/posts')->with('success', 'Post Created');
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'title' => 'required' , 'description' => 'required' , 'colour' => 'required' , 'found_location' => '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 -> type = $ request -> input ( 'type' ) ; $ post -> cover_image = $ fileNameToStore ; $ post -> description = $ request -> input ( 'description' ) ; $ post -> colour = $ request -> input ( 'colour' ) ; $ post -> found_location = $ request -> input ( 'found_location' ) ; $ post -> user_id = auth ( ) -> user ( ) -> id ; $ post -> save ( ) ; return redirect ( '/posts' ) -> with ( 'success' , 'Post Created' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//handling the file upload //get filename and extension // getting the file name only //just ext //filename to store //uploading the image //create post //$post->image = $request->input('image');