2016-03-07 87 views
0

我想返回一个JSON响应,其中包含每个帐户有一个货币(名称,费率)的帐户数组。返回多个模型的JSON响应

我走到这一步:

[ 
    { 
    id: 10001, 
    currency_id: 1, 
    amount: 11000, 
    currency: { 
     id: 1, 
     name: "Dollar", 
     rate: 5.1 
    } 
    } 
] 

,但我需要什么:

[ 
    { 
    id: 10001, 
    currency: { 
     id: 1, 
     name: "Dollar", 
     rate: 5.1 
    }, 
    amount: 11000, 
    } 
] 

currency.php型号:

<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 

class currency extends Model { 

    protected $fillable = array('name', 'rate'); 

    public function account(){ 
     return $this->hasOne('App\account', 'account'); 
    } 
} 

account.php型号:

<?php namespace App; 

use Illuminate\Database\Eloquent\Model; 

class account extends Model { 

    protected $fillable = array('client_id', 'currency_id', 'branch_id', 'credit', 'debit', 'type'); 

    public function currency() 
    { 
     return $this->belongsTo('App\currency'); 
    } 
} 

和我的AccountController:

public function index() 
    { 
     $accounts = Account::with('currency')->get(); 

     return $accounts; 
    } 
+0

什么 的文档('currency') - > get();'(')'; $('''''''''''''''''''') – Wistar

回答