php - Using parseincludes in Laravel5 Fractal -
struggling using parseincludes in https://github.com/thephpleague/fractal.
i have 2 tables, property , weeks. each property has many weeks. using fractal can return property item collection of weeks. want use parseincludes, return of weeks optional.
propertytransformer.php
<?php namespace app\transformer; use app\models\property; use league\fractal\transformerabstract; class propertytransformer extends transformerabstract { protected $availableincludes = [ 'week' ]; public function transform(property $property) { return [ 'id' => (int) $property['propertyid'], 'propertyname' => $property['propertyname'], 'exactbeds' => (int) $property['exactbeds'], 'weeks' => $property->week ]; } /** * include week * * @return league\fractal\itemresource */ public function includeweek( property $property ) { $week = $property->week; return $this->item($week, new weektransformer); } }
weektransformer.php
<?php namespace app\transformer; use app\models\week; use league\fractal; class weektransformer extends fractal\transformerabstract { public function transform(week $week) { return [ 'week' => $week['week'], 'available' => $week['available'], 'price' => (int) $week['price'], ]; } }
my propertycontroller.php
<?php namespace app\http\controllers\api\v1; use app\http\requests; use app\models\week; use illuminate\support\facades\response; use app\models\property; use league\fractal; use league\fractal\manager; use league\fractal\resource\collection collection; use league\fractal\resource\item item; use app\transformer\propertytransformer; class propertycontroller extends \app\http\controllers\controller { public function show($id) { $property = property::with('bedroom')->with('week')->find($id); $fractal = new fractal\manager(); if (isset($_get['include'])) { $fractal->parseincludes($_get['include']); } $resource = new fractal\resource\item($property, new propertytransformer); //$resource = new fractal\resource\collection($properies, new propertytransformer); return $fractal->createdata( $resource )->parseincludes('weeks')->tojson(); }
i following error on parseincludes:-
method 'parseincludes' not found in class \league\fractal\scope
i'm following guide here on transformers - http://fractal.thephpleague.com/transformers/
i think going wrong somewhere here says:-
these includes available can never requested unless manager::parseincludes() method called:
<?php use league\fractal; $fractal = new fractal\manager(); if (isset($_get['include'])) { $fractal->parseincludes($_get['include']); }
if remove parseincludes, don't error, property data collection of weeks, ?include=week doesn't work optionally it.
your problem in line:
return $fractal->createdata( $resource )->parseincludes('weeks')->tojson();
createdata()
returns \league\fractal\scope
, has no parseinlcudes
method.
you've called parseincludes
here:
if (isset($_get['include'])) { $fractal->parseincludes($_get['include']); }
so remove second call in return
statement:
return $fractal->createdata($resource)->tojson();
Comments
Post a Comment