Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TwoMinz's avatar

image path/file is not being fetched

I am currently trying to fetch the image being uploaded from the blade file. The image will be then saved into a json for a pre-save function before applying it to its respective table. However, when I'm trying to upload the image, there's no image being uploaded nor the 'faq_section_img' being present in the dd($request->all()).

here is the controller

public function update(Request $request, $id) {

    //dd($request->file('images'));
    dd($request->all());
    $company = CompanyRev::findOrFail($id);

    if ($request->has('status')) {
        $company->update(['is_deleted' => $request->input('status')]);

        return redirect()->route('company.index')->with('message', 'Company status updated successfully!');
    }

    if ($request->has('pre_save')) {
        $data = $request->except(['_token', 'pre_save']);

        // Handle file uploads manually
        if ($request->hasFile('images')) {
            foreach ($request->file('images') as $key => $file) {
                if ($file->isValid()) {
                    $fileName = str_replace(' ', '_', $company->company_name);
                    $fileExt = $file->getClientOriginalExtension();

                    // If key is mission or vision, name accordingly
                    if (in_array($key, ['mission', 'vision'])) {
                        $imageName = "{$fileName}_{$key}Img.{$fileExt}";
                    } else {
                        $imageName = "{$fileName}_image_{$key}.{$fileExt}";
                    }

                    $folder = public_path("storage/{$request->type}_images");
                    if (!file_exists($folder)) {
                        mkdir($folder, 0755, true);
                    }

                    $fullPath = $folder . '/' . $imageName;

                    if (file_exists($fullPath)) {
                        unlink($fullPath); // delete the existing file
                    }

                    $file->move($folder, $imageName);
                    $path = "{$request->type}_images/" . $imageName;

                    $data['images'][$key] = $path;

                }
            }
        }

        // Now safe to encode to JSON
        PreSave::updateOrInsert(
            [
                'companyId' => $company->id,
                'section' => $request->type,
            ],
            [
                'data' => json_encode($data),
                'updated_at' => now(),
            ]
        );

        return redirect()->back()->with('message', 'Pre-saved successfully!');
    }

    if ($request->has('global_update')) {
        return $this->applyPreSave($id);
    }
    return $this->applyPreSave($id); //If there's no pre-save made, all the current pre-save data will be applied.

}

public function preSave(Request $request, $id) {
    //dd($request->file('images'));

    $company = CompanyRev::findOrFail($id);
    $validated = $request->validate([
        'companyId'=>$company->id,
        'section'=>'required|string',
        'data'=>'required|array',
        'images' => 'array',
        'images.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
    ]);

    if ($request->hasFile('images')) {
        foreach ($request->file('images') as $key => $file) {
            if ($file->isValid()) {
                $fileName = str_replace(' ', '_', $request->company_name);
                $fileExt = $file->getClientOriginalExtension();

                // If key is mission or vision, name accordingly
                if (in_array($key, ['mission', 'vision'])) {
                    $imageName = "{$fileName}_{$key}Img.{$fileExt}";
                } else {
                    $imageName = "{$fileName}_image_{$key}Img.{$fileExt}";
                }

                $folder = public_path("storage/{$request->type}_images");
                if (!file_exists($folder)) {
                    mkdir($folder, 0755, true);
                }

                $file->move($folder, $imageName);
                $path = "{$request->type}_images/" . $imageName;

                $data['images'][$key] = $path;
            }
        }
    }


    $data = $validated['data'];

    PreSave::updateOrInsert(
        [
            'companyId'=>$validated['companyId'],
            'section'=>$validated['section'],
        ],
        [
            'data' => json_encode($data),
            'updated_at'=>now(),
        ]
    );
}

public function applyPreSave($companyId) {
    //dd($request->all());
    $preSavedData = PreSave::where('companyId', $companyId)->get();


    if ($preSavedData->isEmpty()) {
        return redirect()->back()->with('error', 'No pre-saved data found!');
    }

    foreach ($preSavedData as $update) {
        $data = json_decode($update->data, true);


        switch ($update->section) {
            case 'company':

                CompanyRev::findOrFail($companyId)->update($data);
                break;

            case 'faqs':
                $imagePaths = $data['images']['faq_section_img'] ?? null;

                $faqRev = FaqRevs::updateOrCreate(
                    ['companyId'=>$companyId],
                    [
                        'faq_section_description' => $data['description'] ?? '',
                        'faq_section_img' => $imagePaths,
                        'updated_at' => now()
                    ]
                );


                FaqDataRev::where('faqs_section_id', $faqRev->id)->delete();

                if (!empty($data['faqs'])) {
                    foreach ($data['faqs'] as $faq) {
                        FaqDataRev::create([
                            'faqs_section_id' => $faqRev->id,
                            'faq_title' => $faq['question'] ?? '',
                            'faq_description' => $faq['answer'] ?? '',
                            'updated_at' => now(),
                        ]);
                    }
                }
                break;

            default:
                return redirect()->back()->with('error', 'Invalid section type in pre-save data.');
        }
    }

    PreSave::truncate();
    return redirect()->route('company.index')->with('message', 'Company updated successfully!');

}'

and here is the blade code that i am using

@php $faqImage = $faqRev->faq_section_img ?? null; @endphp

                    <div>
                        <div class="mb-8 border border-gray-200 rounded-lg p-6 bg-gray-50 h-full flex flex-col justify-center">
                            <h3 class="text-lg font-semibold text-gray-800 mb-4">FAQs Image</h3>
                    
                            <div class="flex flex-col items-center w-full h-full">
                                <div class="w-72 h-80 sm:w-64 sm:h-64 bg-gray-100 rounded-md border border-gray-300 flex items-center justify-center">
                                    <img id="faq-image-preview" class="max-w-full max-h-full object-contain {{ $faqImage ? '' : 'hidden' }}" 
                                        src="{{ $faqImage ? asset('storage/'. $faqImage) : '' }}" alt="FAQ Image"/>
                                    <span id="no-image-text" class="text-gray-500 text-sm {{ $faqImage ? 'hidden' : '' }}">No image</span>
                                </div> 
                    
                                <div class="mt-4 text-center">
                                    <p class="text-sm text-gray-700 font-medium">Upload FAQs Image</p>
                                    <p class="text-xs text-gray-500">SVG, PNG, JPG, or GIF (Max: 300×300px)</p>
                    
                                    <input type="file" id="image-upload" name="images[faq_section_img]" accept="image/*" class="hidden">
                                    <label for="image-upload" class="mt-2 inline-block px-4 py-2 bg-[#5850EC] text-white rounded-lg cursor-pointer text-sm">
                                        Choose Image
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>
0 likes
7 replies
TwoMinz's avatar

@Snapey this is what i use in my form header

<form id="edit-faqs-form" action="{{ route('company.update', $company->id) }}" method="POST" enctype="multipart/form-data">

martinbean's avatar

@twominz Can you re-format the code in your original post? You should wrap all of the code in three backticks:

​```
// Your code here...
​```
Snapey's avatar

You are trying to save an array of image paths in your model?

                $faqRev = FaqRevs::updateOrCreate(
                    ['companyId'=>$companyId],
                    [
                        'faq_section_description' => $data['description'] ?? '',
                        'faq_section_img' => $imagePaths,
                        'updated_at' => now()
                    ]
                );

These are temporary paths. They are not available once the request is complete.

You need to move each file into storage and then record that path.

https://laravel.com/docs/12.x/filesystem#file-uploads

Please or to participate in this conversation.