pranshu's avatar

Getting same records multiple times, how to modify and correct my transformtaion

I have following tables

  1. categories
  2. contents
  3. category_content(pivot with category_id, content_id)
  4. images
  5. tags
  6. contents_tag(pivot with category_id, tag_id)

I want to show the contents filtered through the transformer when anyone call the for a single category content.

My controller CategoryController.php

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $category = Category::find($id)->contentsWithRelationship()->paginate(10);

        if (!$category) {
            return $this->respondNotFound('Category does not exist.');
        }
        return $this->respond(['data' => $this->categoryTransformer->transformCollection($category->all())]);
    }

Model : Category.php

    /**
     * Get Cotents from the category.
     *
     * @return void
     */
    public function contents()
    {
        return $this->belongsToMany('App\Models\Content');
    }

    /**
     * Get Contents from the category.
     *
     * @return void
     */
    public function contentsWithRelationship()
    {
        return $this->contents('App\Models\Content')->with('tags', 'images');
    }

Transformer Interface: Transformer.php

    abstract class Transformer
    {
        /**
         * Transform a collection
         *
         * @param $item
         * @return array
         */
        public function transformCollection($item)
        {
            return array_map([$this, 'transform'], $item);
        }
        
        /**
         * Transform items
         *
         * @param $item
         * @return mixed
         */
        abstract public function transform($item);
    }

and my Category Transformer below: CategoryTransformer.php

    class CategoryTransformer extends Transformer
    {
        /**
         * Transformer Category.
         *
         * @param $category
         * @return array
         */
        public function transform($category)
        {
            $images_array = array_map([$this, 'transformImages'], $category['images']->toArray());
            $tags_array = array_map([$this, 'transformTags'], $category['tags']->toArray());

            return [
                'title' => $category['title'],
                'description' => $category['description'],
                'image' => $category['image'],
                'status' => $category['status'],
                'images'=> $images_array,
                'tags' => $tags_array,
            ];
        }

        /**
         * this method will transform the image.
         *
         * @param array $image
         * @return void
         */
        public function transformImages($image)
        {
            return [
                'id' => (int) $image['id'],
                'title' => $image['title'],
                'description' => $image['description'],
            ];
        }

        /**
         * This method will transform the tags.
         *
         * @param [type] $image
         * @return void
         */
        public function transformTags($tag)
        {
            return [
                'id' => (int) $tag['id'],
                'title' => $tag['title'],
            ];
        }
    }

I find the solution for the relationship transformation but I am not able to apply it here. Can anybody help me to fix.

how can I archive my result ?

I am getting single record 10 times when I use code as explained above. Sample json below.

    {
    "data": [
    {
    "title": "sint",
    "description": "Vitae totam qui voluptatem consequatur sapiente id. Nesciunt eveniet accusamus laborum eum a. Natus autem assumenda omnis voluptate omnis. At consequatur est non rerum.",
    "image": null,
    "status": "Active",
    "images": [
    {
    "id": 4,
    "title": "doloremque.png",
    "description": "Dolor rem vitae et omnis enim autem ut. Veritatis eius fugit consequatur quo occaecati facere. Ea natus labore voluptatem aut. Id sit qui dolorem ipsum non qui vel."
    },
    {
    "id": 137,
    "title": "enim.png",
    "description": "Soluta nihil quas corrupti eaque illo. Non non beatae placeat amet voluptas inventore."
    },
    {
    "id": 240,
    "title": "repellendus.png",
    "description": "Recusandae at dolores modi et iure nulla. Maxime qui officiis ratione qui delectus ab quia facere. Et est ipsa unde illo qui assumenda inventore. Blanditiis quo numquam odio ipsam veniam cum."
    }
    ],
    "tags": [
    {
    "id": 1,
    "title": "est"
    },
    {
    "id": 19,
    "title": "qui"
    },
    {
    "id": 42,
    "title": "fuga"
    },
    {
    "id": 27,
    "title": "mollitia"
    },
    {
    "id": 45,
    "title": "perferendis"
    },
    {
    "id": 23,
    "title": "dolor"
    },
    {
    "id": 19,
    "title": "qui"
    }
    ]
    },

Also how can I create transformation for images and tags with my content for specified category. I don't want to remove pagination and want to arrange the transformation, I am getting what I need but need help to complete transformation class. How can I add blow code it also give me error for array

    $images_array = array_map([$this, 'transformImages'], $category['images']->toArray());
    $tags_array = array_map([$this, 'transformTags'], $category['tags']->toArray());

Please help me to solve this issue.

0 likes
0 replies

Please or to participate in this conversation.