jabrij93's avatar

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type bigint: "download-documents" CONTEXT: unnamed portal parameter $1 = '...'

I am trying to implement download all files functionality in my code. I've referred to some links in stackoverflow to implement this. However, I am still struggling to make this works, below are the download controller,

public function downloadDocuments($id)
    {
        $documents = DB::table('product_documents')->where('id', $id)->first();
        $path2 = $documents->file_path;
        $files = array(public_path($path2));
        $zip = new ZipArchive();
        $zip_name = time() . "documents.zip"; // Zip name
        $zip->open($zip_name,  ZipArchive::CREATE);
        foreach ($files as $file) {
            echo $path = "public/Product/Documents/" . $file;
            if (file_exists($path)) {
                $zip->addFromString(basename($path),  file_get_contents($path));
            } else {
                echo "file does not exist";
            }
        }
        $zip->close();
    }

Here is the button and route for the download,

<Link href={route('product-documents.download', ['id', document.id])}>
                            <span
                                className="flex items-center justify-center"
                                data-tooltip-id="my-tooltip"
                                data-tooltip-content="Download dokumen"
                            >
                                <DocumentArrowDownIcon className="h-4 w-4" />
                            </span>
                        </Link>
Route::get('/download-documents', [ProductsController::class, 'downloadDocuments'])->name('product-documents.download');

Below are the controller when storing all the documents/images. For experiment, I only want to download multiple files of the documents. I tried again and again but it keeps on failing. Please tell me how can I fix this ? Thank you

public function store(Request $request)
    {
        $data = $this->validate($request, [
            'name' => 'required',
            'description' => 'required|max:150',
            'category_id' => 'required',
            'type_id' => 'required',
            'price' => 'required|numeric|min:0|not_in:0',
            'start_date' => 'required',
            'end_date' => 'required',
            'is_active' => 'required',
            'images.*' => 'image|max:2048', // max file size: 2MB
            'documents.*' => 'file|max:2048', // max file size: 2MB
        ]);

        $product = DB::table('products')
            ->insertGetId([
                'name' => $data['name'],
                'description' => $data['description'],
                'type_id' => $data['type_id'],
                'category_id' => $data['category_id'],
                'price' => $data['price'],
                'start_date' => $data['start_date'],
                'end_date' => $data['end_date'],
                'is_password' => $request['is_password'],
                'is_stamping' => $request['is_stamping'],
                'created_at' => now(),
            ]);

        // handle image uploads
        if ($request->hasFile('images')) {
            $i = 1;
            foreach ($request->file('images') as $image) {
                $name = $request['name'] . '-' . $i;
                $now = new DateTime();
                $fileName = Str::slug($request['name']) . '-' . $now->format('dmYHis') . '-' . $i . '.' . $image->getClientOriginalExtension();
                //$fileName = Str::slug($request['name']) . '-' . time() . '.' . $image->getClientOriginalExtension();
                $path = $image->storeAs('public/Product/Images', $fileName);
                DB::table('product_images')->insert([
                    'product_id' => $product,
                    'name' => $name,
                    'file_path' => Storage::url($path),
                    'created_at' => now(),
                ]);
                $i++;
            }
        }

        // handle document uploads
        if ($request->hasFile('documents')) {
            $i = 1;
            foreach ($request->file('documents') as $document) {
                $name = $request['name'] . '-' . $i;
                $now = new DateTime();
                $fileName = Str::slug($request['name']) . '-' . $now->format('dmYHis') . '-' . $i . '.' . $document->getClientOriginalExtension();
                //$fileName = Str::slug($request['name']) . '-' . time() . '.' . $document->getClientOriginalExtension();
                $path = $document->storeAs('public/Product/Documents', $fileName);
                DB::table('product_documents')->insert([
                    'product_id' => $product,
                    'name' => $name,
                    'file_path' => Storage::url($path),
                    'created_at' => now(),
                ]);
                $i++;
            }
        }

        $now = Carbon::now();
        $month = $now->format('m');
        $year = $now->format('Y');

        DB::table('log_audits')->insert([
            'module_name' => 'Jualan',
            'description' => 'Simpan Produk ' . $data['name'],
            'user_id' => auth()->user()->id,
            'created_at' => $now,
            'month' => $month,
            'year' => $year,
        ]);
        return Redirect::route('produk.index');
    }

After writing the code and tried to run it. I get the following error,

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type bigint: "download-documents" CONTEXT: unnamed portal parameter '$'1 = '...'

select 
"products".*, 
"product_images"."file_path" as "image_path", 
"product_documents"."file_path" as "document_path" 

from 
"products" 
left join "product_images" on "products"."id" = "product_images"."product_id" 
left join "product_documents" on "products"."id" = "product_documents"."product_id" 

where 
"products"."id" = download-documents

I am sure that I may have missed something in my code or probably I wrote and referenced it to the wrong things. Please help

0 likes
1 reply
LaryAI's avatar
Level 58

The error message suggests that the $id parameter passed to the downloadDocuments method is not a valid integer. This is because the route definition is missing the parameter name. Instead of ['id', document.id], it should be ['id' => document.id].

Here's the corrected code for the link:

<Link href="{{ route('product-documents.download', ['id' => document.id]) }}">
    <span
        className="flex items-center justify-center"
        data-tooltip-id="my-tooltip"
        data-tooltip-content="Download dokumen"
    >
        <DocumentArrowDownIcon className="h-4 w-4" />
    </span>
</Link>

Also, in the downloadDocuments method, the $path variable is being set incorrectly. It should be set to $file instead of $path2 . $file. Here's the corrected code:

public function downloadDocuments($id)
{
    $documents = DB::table('product_documents')->where('id', $id)->first();
    $path2 = $documents->file_path;
    $files = array(public_path($path2));
    $zip = new ZipArchive();
    $zip_name = time() . "documents.zip"; // Zip name
    $zip->open($zip_name,  ZipArchive::CREATE);
    foreach ($files as $file) {
        echo $path = "public/Product/Documents/" . $file;
        if (file_exists($file)) {
            $zip->addFromString(basename($file),  file_get_contents($file));
        } else {
            echo "file does not exist";
        }
    }
    $zip->close();
}

Please or to participate in this conversation.