datarecall
1 week ago
104
5
General

Help With Resource

Posted 1 week ago by datarecall

In my product controller I have

   public function index(Product $product)
    {
        $ratings = $this->getRatings($product);
        
        return RatingResource::collection($ratings);
    }
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class RatingResource extends JsonResource {
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'user'  => $this->user,
            'product' => NEED TO HAVE $PRODUCT HERE
            'diary' => [
                'title' => $this->name,
                'url'   => $this->route,
                'comment_count' => $this->comment_count,
                'weeks_count' => $this->weeks_count
            ],
            'created_at' => $this->pivot->created_at,
            'score' => $this->pivot->score,
            'body'  => $this->pivot->body,
        ];
    }
}

In this resource, I need access to the $product since I need to send the product back when an ajax request is hit

Please sign in or create an account to participate in this conversation.