class TwitterTransformer extends TransformerAbstract
{
protected $availableIncludes = ['twitterpic','consumer'];
public function transform(Twitter $twitter)
{
return $twitter->attributesToArray();
}
public function includeConsumer(Twitter $twitter){
if (! ($twitter->consumer)) {
return $this->null();
}
return $this->item($twitter->consumer, new TwitterConsumerTransformer());
}
public function includeTwitterpicr(Twitter $twitter)
{
if (! $twitter->twitterpics) {
return $this->null();
}
return $this->item($twitter->twitterpics, new TwitterpicTransformer());
}
}
TwitterpicTransforms.php
use App\Twitterpic;
use League\Fractal\TransformerAbstract;
class TwitterpicTransformer extends TransformerAbstract
{
public function transform(Twitterpic $twitterpic){
return
[
'id'=>$twitterpic['id'],
'image_url'=>$twitterpic['pic'],
];
}
}
单结果中 include 的字段为何还是无法加载,困扰了一下午,我照着文档看了好多遍。。。没毛病啊
{
"data": [
{
"id": 4,
"content": "A et suscipit est sed.",
"clubname": "Dr. Duane Emard III",
"club_id": 17,
"consumer_id": 10,
"likes": 0,
"created_at": "2017-08-26 08:12:05",
"updated_at": "2017-08-26 08:12:05"
},
{
"id": 5,
"content": "Tempora nesciunt et delectus cumque eos et architecto.",
"clubname": "Patsy Rutherford",
"club_id": 1,
"consumer_id": 6,
"likes": 0,
"created_at": "2017-08-26 08:12:05",
"updated_at": "2017-08-26 08:12:05"
}
]
}