我有以下
public function show()
{
$users_id = Request::segment(4);
// $this->user_bank_details_repository->setPresenter(new UserBankAccountPresenter);
$account = $this->user_bank_details_repository->findByField('users_id', $users_id, $columns = ['user_bank_details_id','bank_name','bank_account_number','bank_ifsc_code','beneficiary_name','bank_account_type','bank_branch','created_at']);
if(!$account)
{
return $this->response->noContent();
}
return $this->response->item($account, new UserBankAccountTransformer);
}
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Api\V1\Transformers;
use App\Entities\UserBankDetails;
use League\Fractal\TransformerAbstract;
class UserBankAccountTransformer extends TransformerAbstract {
public function transform(UserBankDetails $bank_details)
{
return [
'id'=> (int) $bank_details->user_bank_details_id,
'bank_name' => $bank_details->bank_name,
'bank_account_number' => (int) $bank_details->bank_account_number,
'bank_account_type' => $bank_details->bank_account_type,
'bank_beneficiary_name' => $bank_details->beneficiary_name,
'bank_branch'=> $bank_details->bank_branch
];
}
}
我正在使用存储库模式设计并将dingo用于我的REST框架。
我总是得到回复
{
"user_bank_details": [
{
"user_bank_details_id": 1,
"bank_name": "jbjb",
"bank_account_number": "939393933939",
"bank_ifsc_code": "ABCD0000047",
"beneficiary_name": "Gaf",
"bank_account_type": "savings",
"bank_branch": "Mad(E)",
"created_at": "2015-12-23 17:05:39"
}
]
}
而不是变换后的json。我也没有任何错误。尝试使用
return $this->response->item($account, new UserBankAccountTransformer::class);
但是反应仍然没有得到改变。我尽我所能,但我没有得到它的工作:(
答案 0 :(得分:0)
即使这个问题已经过去一年多了,但是为了任何会遇到类似问题的人,这里的嫌疑人是你使用item
代替collection
的事实。
如果$account
包含一个集合,则表示此答案是正确的:即:
return $this->response->item($account, new UserBankAccountTransformer::class);
应该是:
return $this->response->collection($account, new UserBankAccountTransformer::class);
PS:我也在Laravel 5.2上遇到过这个问题。*这就是为什么我觉得这可能会有所帮助。