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;
}
什麼 的文檔('currency') - > get();'(')'; $('''''''''''''''''''') – Wistar