我在 Laravel 中有两个模型,我使用 Laravel 作为 API。
Wallet = [name, wallet_category_id, user_id]
Wallet_Category = [id, name]
我在控制器中使用 get() 调用所有钱包,如何在输出中添加钱包类别名称?
Wallet::where('user_id', $request->get('user_id'))->get()
输出将类似于:
[
{
"id": 12,
"user_id": 2,
"name": "wallet1",
"wallet_category_id" : 1,
"created_at": "2020-03-09 22:40:29",
"updated_at": "2020-03-09 22:40:29"
},
{
"id": 13,
"user_id": 2,
"name": "wallet2",
"wallet_category_id" : 2,
"created_at": "2020-03-10 13:57:37",
"updated_at": "2020-03-10 13:57:37"
}
]
我想要这样的输出:
[
{
"id": 12,
"user_id": 2,
"name": "wallet1",
"wallet_category_id" : 1,
"created_at": "2020-03-09 22:40:29",
"updated_at": "2020-03-09 22:40:29",
"category" : "cat_1"
},
{
"id": 13,
"user_id": 2,
"name": "wallet2",
"wallet_category_id" : 2,
"created_at": "2020-03-10 13:57:37",
"updated_at": "2020-03-10 13:57:37",
"category" : "cat_2"
}
]
可能吗?
之前谢谢。
HUH函数
慕码人8056858