Neeraj1005's avatar

Image upload not working

I'm trying to create an API to store the value but my image store is not working, For image upload, I'm using polymorphic table with hasMany relations. But I don't why it is not storing. plz check my code what i did wrong...? THis is my api controller

    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'product_name' => 'required',
            'product_category_id' => 'required|not_in:0',
            'sub_category_id' => 'required|not_in:0',
            'company_id' => 'required|not_in:0',
            'product_quantity' => 'nullable|numeric',
            'unit_id' => 'nullable|numeric|not_in:0',
            'purchase_price' => 'nullable',
            'city' => 'nullable|string|max:255',
            // 'isChecked' => 'required|accepted',
            'details' => 'required|string|min:5',
            'images.*' => 'mimes:jpg,jpeg,png,bmp,gif,svg,webp,pdf,docx|max:1024',
        ]) + [
            'user_id' => auth()->user()->id,
        ];

        // $storedData = auth()->user()->rfqs()->create($validatedData);
        $storedData = Rfq::create($validatedData);

        if ($request->hasFile('images')) {

            foreach ($request->file('images') as $photo) {
                dd($photo); // this is also not working 
                $path_url = $photo->storePublicly('rfqs', 'public');
                $storedData->images()->create([
                    'file_name' => $photo->getClientOriginalName(),
                    'mime_type' => $photo->getMimeType(),
                    'file_path' => $path_url,
                ]);
            }
        }

        $response =  new RfqResource($storedData);

        return response()->json(['status' => 'success', 'data' => $response]);

    }

THis direct show me the output....

0 likes
5 replies
Tray2's avatar

First make sure that your form has this

enctype="multipart/form-data

Then try removing this line from your validation

'images.*' => 'mimes:jpg,jpeg,png,bmp,gif,svg,webp,pdf,docx|max:1024',

I also suggest cheking out intervention

http://image.intervention.io/

Neeraj1005's avatar

@tray2 If I do for this single image it is working but for multiple image it is not working.

   if ($request->hasFile('images')) {
            // foreach (request('images') as $photo) {
                $path_url = request('images')->storePublicly('rfqs', 'public');
                $storedData->images()->create([
                    'file_name' => request('images')->getClientOriginalName(),
                    'mime_type' => request('images')->getMimeType(),
                    'file_path' => $path_url,
                ]);
            // }
        }
Neeraj1005's avatar

i did the same for image uploading but foreach loop is not working.... Is there any other things to do...?

    if ($request->hasFile('images')) {

            foreach ($request->file('images') as $photo) {
                dd($photo); // this is also not working 
                $path_url = $photo->storePublicly('rfqs', 'public');
                $storedData->images()->create([
                    'file_name' => $photo->getClientOriginalName(),
                    'mime_type' => $photo->getMimeType(),
                    'file_path' => $path_url,
                ]);
            }
        }

Please or to participate in this conversation.