lara_vel's avatar

Data not stored on database, image upload failed

I have a post with title, texts & image. I can successfully create title, text and upload image to folder but I can't save the image path to database. Here is my code.

 public function save(Request $request, Post $post)
    {       
        
        $this->validate(request(),
        [
            'title' => 'required',
            'image' => 'image|mimes:jpg,png,jpeg'
        ],
        
        [
            'title.required' => 'What is your post about?',
            'image.image' => 'Image Required'
        ]);
        
        if($request->hasFile('image'))
        {
            $file = $request->file('image');
            $fileNameExt = $request->file('image')->getClientOriginalName();
            $fileName = pathinfo($fileNameExt, PATHINFO_FILENAME);
            $fileExt = $request->file('image')->getClientOriginalExtension();
            $fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
            Image::make($file)->resize(600, 600)->save( public_path('media/' . $fileNameToStore));
            $post->image = $fileNameToStore;

        auth()->user()->save(
            new Post(request(['title', 'text', 'image']))
        );
    }
        return redirect ('/');
    }

But it does not store image path to image column of database. Instead tmp data like C:\xampp\tmp\phpC549.tmp stored. What is wrong here?

0 likes
2 replies
JackJones's avatar

You're saving the image from the request, which is the path to the temp directory

auth()->user()->save(
            new Post(request(['title', 'text', 'image']))
        );
lara_vel's avatar

What is the solution to store the real path?

Please or to participate in this conversation.