当前位置:  开发笔记 > 编程语言 > 正文

Laravel 5 - 分形变压器 - 向变压器发送参数以缩小响应范围

如何解决《Laravel5-分形变压器-向变压器发送参数以缩小响应范围》经验,为你挑选了1个好方法。

所以我有一个名为VIP的模型,它有很多与之相关的信息.因此,当我们去路线时,vip/{id}我会返回大部分信息.但是,当我去的时候,vips/{per-page}我不想返回所有数据,因为API会以指数方式减速.相反,我宁愿只返回一些更基本的东西.

为了做到这一点,我有一个Transformer,在顶部创建基本响应,然后进入下面的一些更复杂的关联,然后返回所有.我希望向变换器发送某种类型的标志,如果为真则只返回基本响应.但是,我正在使用分形,似乎无法找到一种能够将此标志发送到变压器的方法.

VipsController @ index:

   /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $perPage = 15)
    {
        $page = $request->input('page');
        $vip = Cache::tags(['vips'])->remember("vip.index.$perPage.$page", 60, function() use ($perPage){
            Log::info('getting data from the database');
            return $this->vip->paginate($perPage, ['*'], ['gender','status','local','jobClass','nonMember','initiationDate','pensioner']);
        });

        return $this->fractal->paginatedCollection($vip, new VipTransformer());
    }

VipTransformer @转换:

    public function transform(Vip $vip)
    {
        $response = [
            'id' => $vip->vip_id,
            'first_name' => isset($vip->first_name) ? $vip->first_name : '',
            'last_name' => isset($vip->last_name) ? $vip->last_name : '',
            'birth_date' => isset($vip->birth_date) ? $vip->birth_date : '',
            'deceased_date' => isset($vip->deceased_date) ? $vip->deceased_date : '',
            'divorced_date' => isset($vip->divorced_date) ? $vip->divorced_date : '',
            'marriage_date' => isset($vip->marriage_date) ? $vip->marriage_date : '',
            'initiation_date' => isset($vip->initiationDate->date_value) ? $vip->initiationDate->date_value : '0000-00-00',
            'gender' => isset($vip->gender->code_description) ? $vip->gender->code_description : '',
            'status' => isset($vip->status->code_description) ? $vip->status->code_description : '',
            'local' => isset($vip->local) ? "{$vip->local->local_code} - {$vip->local->local_name}" : '',
            'jobClass' => isset($vip->jobClass) ? $vip->jobClass->code_description : '',
            'nonMember' => isset($vip->nonMember) ? $vip->nonMember->code_description : '',
            'age' => isset($vip->birth_date) && $vip->birth_date !== '0000-00-00' ? Carbon::parse($vip->birth_date)->age : 0,
        ];

        // Grab retirement date from pension tables
        $response['retirement_date'] = $vip->pensioner->retirement_date ?? '0000-00-00';

        // Grab the associated information for the vip profile
        $reciprocals = [];
        $addresses = [];
        $phone_numbers = [];
        $emails = [];

        foreach($vip->addresses as $address) {
            $addresses = [
                'address1' => $address->address_1 ?? '',
                'address2' => $address->address_2 ?? '',
                'city' => $address->city ?? '',
                'zip' => $address->zip_code ?? '',
                'country' => $address->country->code ?? '',
                'state' => $address->state->state_province_code ?? '',
                'is_default' => $address->is_default ?? false,
            ];
        }

        // TODO: Add record status trait to each

        foreach ($vip->phones as $phone) {
            $phone_numbers[] = [
                'extension' => $phone->extension ?? '',
                'number' => $phone->phone_number ?? '',
                'is_default' => $phone->is_default ?? false,
            ];
        }

        foreach ($vip->emails as $email) {
            $emails[] = [
                'email_address' => $email->email_address ?? '',
                'is_default' => $email->is_default ?? false,
            ];
        }

        foreach ($vip->reciprocalMembers as $member) {
            $reciprocals[] = $member->effective_date . ' - ' . $member->fund->fund_abbreviation . ' - ' . $vip->local->local_name;
        }

        $response['addresses'] = $addresses;
        $response['phone_numbers'] = $phone_numbers;
        $response['emails'] = $emails;
        $response['reciprocals'] = $reciprocals;


        return $response;
    }

有没有办法让我使用Fractal向变压器发送标志?还有另一种更好的方法吗?

更新:

答案有效,但我这样做了....随意按你的意愿去做:

变压器:

protected $indexRequest;

public function __construct($indexRequest = null)
{
    $this->indexRequest = $indexRequest ?? false;
}

分形召唤:

return $this->fractal->paginatedCollection($vips, new VipTransformer(true));



1> Fabio Antune..:

我从来没有使用过分形,但是你不能像在VipTransformer课堂上添加旗帜一样.

 basicResponseFlag = $flag;

    }

    public function transform(Vip $vip)
    {
        if($this->basicResponseFlag){
            //do basic stuff
        }
    }
}

现在在你的VipsController @ index上:

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(Request $request, $perPage = 15)
{
    $page = $request->input('page');
    $vip = Cache::tags(['vips'])->remember("vip.index.$perPage.$page", 60, function() use ($perPage){
        Log::info('getting data from the database');
        return $this->vip->paginate($perPage, ['*'], ['gender','status','local','jobClass','nonMember','initiationDate','pensioner']);
    });

    $vipTransformer = new VipTransformer();
    $vipTransformer->setFlag(true); //set your transfomer to basic mode
    return $this->fractal->paginatedCollection($vip, $vipTransformer);
}

推荐阅读
贾志军
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有