vendredi 27 octobre 2017

Laravel fractal pass an object or an array to include method

I have a transformer to which I should pass an object in the include method. This is how the transformer looks like:

class ContentTypeTransformer extends Fractal\TransformerAbstract
{
    protected $availableIncludes = [
        'content',
        'supplementaries',
        'intranet'
    ];

    public function transform(ContentType $contentType)
    {
        return [
            'id' => $contentType->id,
            'slug' => $contentType->slug,
            'name' => ucfirst($contentType->name),
        ];
    }

    public function includeContent(ContentType $contentType)
    {
        $content = $contentType->contents;
        return $this->collection($content , new ContentTypePreviewTransformer());
    }

    public function includeIntranet(ContentType $contentType, ParamBag $params = null)
    {
        $userTaxonomies = $params->get('userTaxonomies');
        $content = Content::where('ct_id', $contentType->id)->whereHas('taxonomies', function($q) use ($userTaxonomies){
          $q->whereIn('slug', $userTaxonomies);
        })->get();

        return $this->collection($content , new ContentTypePreviewTransformer());
    }
}

So, I should pass an an array $userTaxonomies, to a method includeIntranet, but not sure how should I do that in the controller:

public function index(Request $request)
{
    $id = Auth::user()->id;
    if (!$id) {
      return;
    }
    $user = User::find($id);

    $this->includes = explode(',', $request->get('include'));
    $this->addDefaultInclude('intranet');

    if (isset($this->includes)) {
        $this->response->getManager()->parseIncludes(array_filter($this->includes));
    }
    $userTaxonomies = $this->getUserTaxonomies($user);
    $contentType = ContentType::where('name', 'intranet-post')->first();

    if (!$contentType) {
        return $this->response->errorNotFound();
    }

    return $this->response->withItem($contentType, new ContentTypeTransformer());
}

How can I pass an array to that includeIntranet method?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2xsrvHS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire