标签: thephpleague-fractal

用于集合的Laravel 5.5 API资源(独立数据)

我想知道是否可以为项目资源和集合资源定义不同的数据.

对于收集我只想发送,['id', 'title', 'slug']但项目资源将包含额外的细节['id', 'title', 'slug', 'user', etc.]

我希望实现以下目标:

class PageResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'user' => [
                'id' => $this->user->id,
                'name' => $this->user->name,
                'email' => $this->user->email,
            ],
        ];
    }
}

class PageResourceCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent thephpleague-fractal laravel-5.5

12
推荐指数
2
解决办法
1万
查看次数

我可以使用转换器来转换来自API而不是来自数据库的数据吗? - Laravel/Fractal

我一直在使用laravel来构建我的API.我使用变换器来转换模型对象的数据.

现在我没有数据库,而是来自API的响应作为数据源,我想将这些数据转换回用户,但我无法这样做.

我的控制器

 public function rocByName(Request $request)
    {
        try {
            $this->roc_by_name_validator->with( $request->all() )->passesOrFail();
            $company_name = $request->input('company_name');
            $result = $this->my_service->getDetailsByName($company_name); //$result has the response object from the API which I want to transform and give it as a response.

             return $this->response->collection($result,new OnboardingTransformer()); //Tried using tranformer like this
        }
        catch (ValidatorException $e) {

            dd($e);

        }

    }
Run Code Online (Sandbox Code Playgroud)

我的变形金刚

<?php

namespace Modules\Onboarding\Transformers;

use League\Fractal\TransformerAbstract;
use App\Entities\OnboardingEntity;  //I dont have an entity since the response is coming from an API!! What will give here?

/** …
Run Code Online (Sandbox Code Playgroud)

php transformer-model laravel dingo-api thephpleague-fractal

11
推荐指数
1
解决办法
2754
查看次数

Laravel Fractal不会从包含关系中返回元

这是我PostTransformer加入关系的地方

public function includeComments(Post $post)
{
    if (($post->is_paid == 1 && $post->haspaid == 1) || ($post->author == $this->params) || ($post->is_paid == 0)){
        $comments = Comment::where('post', $post->id)
            ->where('is_blocked', '=', 0)
            ->select('id',
                'text',
                'author',
                'post',
                'created_at',
                'updated_at',
                'book_id',
                DB::raw("if(comments.author=$this->params,'true','false') as isauthor"))
            ->orderBy('created_at', 'ASC')
            ->paginate(5);
        $commentTransformer = new CommentTransformer($this->params);
        $commentResource = $this->collection($comments, $commentTransformer, 'comment')->setMeta(['total'=>count($comments)]);
        return $commentResource;
    } elseif ($post->is_paid == 1 && $post->haspaid == 0) {
        return $this->null();
    }

}
Run Code Online (Sandbox Code Playgroud)

这应该与meta的评论关系产生.但这里的问题是我没有得到包含关系的meta.如果有人可以帮我解决这个问题.

php json-api laravel-5.3 thephpleague thephpleague-fractal

6
推荐指数
1
解决办法
391
查看次数

口才:API资源与分形

快速提问,Eloquent:API资源和Fractal有什么区别?

对我来说,看起来像是同一件事吗?

laravel laravel-5 thephpleague-fractal

2
推荐指数
1
解决办法
518
查看次数