gpt4 book ai didi

php - Laravel 5 - 分形更改器(mutator) - 将参数发送到更改器(mutator)以缩小响应范围

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:58 30 4
gpt4 key购买 nike

所以我有一个名为 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@transform:

    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));

最佳答案

我从未使用过分形,但你不能做一些事情,比如在你的 VipTransformer 类上添加一个标志。

 <?php

class VipTransformer
{
protected $basicResponseFlag = false;


public function setFlag($flag)
{
$this->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);
}

关于php - Laravel 5 - 分形更改器(mutator) - 将参数发送到更改器(mutator)以缩小响应范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092777/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com