anuzpandey's avatar

Image Stored as .tmp file in database. [Laravel]

I am using a Separate Class to upload my images in Laravel. The image files successfully get uploaded in the desired location but the filename in database is stored as C:\xampp\tmp\php1A3F.tmp.

Controller

// Store Method inside Team Controller
public function store(TeamRequest $request, SingleImageUpload $singleImageUpload) {
        $file = $request->file('photo');
        try {
            $fileName = $singleImageUpload->store($file, getcwd() . '/images/teams/', 400, 400);

            $request->merge(['photo' => $fileName]);
            $request->has('showOnHome')
                ? $request->merge(['showOnHome' => 1])
                : null;

            Team::create($request->all());

            return back()->with('success', 'Team Member Added Successfully.');
        } catch (Throwable $throwable) {
            return back()->with('error', 'Something went wrong. Please try again or contact administrator.');
        }
    }

ImageUploader Class

class SingleImageUpload {

    public function store($file, $path, $width, $height) {
        $fileName = 'protechimage'.time().'.'.$file->getClientOriginalExtension();
        Image::make($file)
            ->fit($width, $height)
            ->save($path . $fileName, 100);
        return $fileName;
    }

}
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

try instead;

Team::create($request->except(['photo','showOnHome']) + ['photo'=>$fileName,'showOnHome'=> $request->has('showOnHome')]);

and remove the merge commands

1 like

Please or to participate in this conversation.