eusapxai's avatar

how to parse array in resource collections

this is my function

public function test(Request $request){
        $products = Product::with(['category', 'unit', 'ratings'])->where('quantity', '>', 0)->get();
        if($request->wantsJson())
        {
            // Return a collection of $users with pagination
            return ProductsResource::collection($products);
        }else{
            return response()->json($products);
        }
    }

ProductsResource

return [
            'product_id' => $this->prd_id,
            'category' => $this->category['en_name'],
            'en_name' => $this->en_name,
            'ur_name' => $this->ur_name,
            'price' => $this->price,
            'product_image' => $this->prd_image,
            'en_unit' => $this->unit['en_unit'],
            'ur_unit' => $this->unit['ur_unit'],
            'ratings' => $this->ratings 
]

the output is

[
    {
        "product_id": 7,
        "category": "Vegetables",
        "en_name": "Tomato",
        "ur_name": "ٹماٹر",
        "price": "130",
        "product_image": "https://farmtohome-dev.com/products/Tomato.png",
        "en_unit": "Kg",
        "ur_unit": "Kg",
        "ratings": [
            {
                "rating_id": 2,
                "prd_id": 7,
                "username": "031495555",
                "review": "",
                "stars": 4,
                "datetime": "2017-11-15 16:52:57"
            },
            {
                "rating_id": 4,
                "prd_id": 7,
                "username": "033451723",
                "review": "",
                "stars": 4,
                "datetime": "2017-11-15 17:41:30"
            },
            {
                "rating_id": 12,
                "prd_id": 7,
                "username": "033180990",
                "review": "",
                "stars": 3,
                "datetime": "2017-11-19 14:45:23"
            },
            {
                "rating_id": 34,
                "prd_id": 7,
                "username": "033442699",
                "review": "trustable people , fresh products",
                "stars": 5,
                "datetime": "2018-10-08 10:50:22"
            },
            {
                "rating_id": 63,
                "prd_id": 7,
                "username": "031208972",
                "review": "Best quality ",
                "stars": 4,
                "datetime": "2019-01-02 11:34:25"
            }
        ]
    },
]

and i want to change the ratings array like this

{
                "rating_id": 63,
                "msisdn": "031212213",
                "review": "Best quality ",
                "stars": 4,
                "datetime": "2019-01-02 11:34:25"
 }
0 likes
4 replies
vandan's avatar

$users = DB::table('users')->get()->toJson();

dd($users);

martinbean's avatar
Level 80

@eusapxai You can use resources in resources. So you could create a “rating resource” and then use that to format your ratings:

class ProductResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'product_id' => $this->prd_id,

            // Snipped

            'ratings' => RatingResource::collection($this->ratings),
        ];
    }
}
class RatingResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'rating_id' => $this->rating_id,

            // And so on
        ];
    }
}

Please or to participate in this conversation.