kikter's avatar

laravel multiple image upload not working

I am trying to save some images to the database but it's not been uploaded to the database how can I solve this

public function store(Request $request) {

        $thread = [
                    "title"  =>  $request->title,
                    "body" => $request->content,
                    "user_id" => auth()->user()->id,
                    "space_id" => $request->space,
                ];
                
            foreach ($request->images as $key => $image) {
            $path = $image->storeAs('gallery', str::random(30));
           $images = Image::make($image)->fit(1280, 720);
           Storage::put($path, (string)$images->encode());

            ThreadImages::create([
                'thread_id' => $thread->id,
                'filename' => $path,
            ]);
        }

        $thread  =  Thread::create($thread);

        return back()->with("success", "Topic has been created");

    }
0 likes
4 replies
ollie_123's avatar

@kikter Have you checked that your $fillable fields in your model include "filename"?

amk's avatar

try with this... @kikter

public function store(Request $request) {

        $thread = [
                    "title"  =>  $request->title,
                    "body" => $request->content,
                    "user_id" => auth()->user()->id,
                    "space_id" => $request->space,
                ];
                
        $thread  =  Thread::create($thread);

        foreach ($request->images as $key => $image) { 
           $path = 'gallery/' . str::random(30);
           $images = Image::make($image)->fit(1280, 720)->save($path);

            ThreadImages::create([
                'thread_id' => $thread->id,
                'filename' => $path,
            ]);
        }

        return back()->with("success", "Topic has been created");

    }
Snapey's avatar

did you check that $request->images contains uploaded files?

are any database records created?

are any files stored in file system?

You are using $thread_id before you have even created the thread.

Please or to participate in this conversation.