Blagoj
Blagoj

Reputation: 1

How to retrieve the URL from an image saved in imageKit?

I need to seed into my products and product_picture table. The below code is populating the products table, uploading the image to imageKit successfully, but fails to populate the product_picture table.

        foreach ($products as $productData) {
            // Create product
            $product = Product::create([
                'name' => $productData['name'],
                'description' => $productData['description'],
                'left_in_stock' => $productData['left_in_stock'],
                'price' => $productData['price'],
                'category_id' => $productData['category_id'],
                'price_range_id' => $productData['price_range_id'],
            ]);

            foreach ($productData['images'] as $imagePath) {
                // Upload image to ImageKit
                $imageKit = new ImageKit(
                    env('IMAGEKIT_PUBLIC_KEY'),
                    env('IMAGEKIT_PRIVATE_KEY'),
                    env('IMAGEKIT_URL_ENDPOINT')
                );

                $fullImagePath = public_path($imagePath);
                $uploadedFile = $imageKit->upload([
                    'file' => fopen($fullImagePath, 'r'),
                    'fileName' => basename($fullImagePath)
                ]);


                // Save product picture
                $productPicture = new ProductPicture;
                $productPicture->product_id = $product->id;
                $productPicture->picture = $uploadedFile['url']; //The problem is here, error:   Cannot               use object of type ImageKit\Utils\Response as array
                $productPicture->save();
            }
        }

I also tried this:

        foreach ($products as $productData) {
            // Create product
            $product = Product::create([
                'name' => $productData['name'],
                'description' => $productData['description'],
                'left_in_stock' => $productData['left_in_stock'],
                'price' => $productData['price'],
                'category_id' => $productData['category_id'],
                'price_range_id' => $productData['price_range_id'],
            ]);

            foreach ($productData['images'] as $imagePath) {
                // Upload image to ImageKit
                $imageKit = new ImageKit(
                    env('IMAGEKIT_PUBLIC_KEY'),
                    env('IMAGEKIT_PRIVATE_KEY'),
                    env('IMAGEKIT_URL_ENDPOINT')
                );

                $fullImagePath = public_path($imagePath);
                $uploadedFile = $imageKit->upload([
                    'file' => fopen($fullImagePath, 'r'),
                    'fileName' => basename($fullImagePath)
                ]);


                // Save product picture
                $productPicture = new ProductPicture;
                $productPicture->product_id = $product->id;
                $productPicture->picture = $uploadedFile['url']; //The problem is here, error:   Call to undefined method ImageKit\Utils\Response::getUrl()
                $productPicture->save();
            }
        }

When I add var_dump($uploadFile):

["url"]=> string(54) "https://ik.imagekit.io/miwdkh85d/Image1__lprlSvaI.jpeg"

Upvotes: 0

Views: 150

Answers (0)

Related Questions