webfuelcode's avatar

Post with image

Saving the post but not the image in the database. File upload works fine.

Please correct it to make the image save in the database.

controller:

public function store(Request $request)
    {
        $validatedData = $this->validate($request, [
            'title'         => 'required|min:3|max:255',
            'img1'         => 'sometimes|image',
            'img2'         => 'sometimes|image',
            'category_id'   => 'required|numeric',
            'description'   => 'required|min:10'
        ]);

        // Post image upload
        if($request->hasFile('img1')){
            $filename = time() . '.' . $request->img1->getClientOriginalExtension();
            $request->img1->storeAs('post_img', $filename, 'my_files');
            
/////////??????????/////////
        }
    
        $validatedData['user_id'] = Auth::id();
    
        $post = Post::create($validatedData);

        return redirect()->route('post.show', ['post' => $post->slug])->withMessage('Post created successfuly!');
    }
0 likes
1 reply
automica's avatar
automica
Best Answer
Level 54

@webfuelcode have you added 'img1' & 'img2' to your $fillable array?

also you should be setting

$validatedData['img1'] = $filename;

to save file path not image to table.

Please or to participate in this conversation.