【发布时间】:2019-04-16 00:12:59
【问题描述】:
有了这个请求:
$apps = DB::select("select
a.id, a.app_name, a.country_id, a.feature_id, a.organization_id, a.status_id,
o.organization_name, f.feature_name
from apps as a
left join organizations as o on o.id = a.organization_id
left join features as f on f.id = a.feature_id
order by app_name
");
$apps = DB::table('apps')->orderBy('app_name')->paginate(10);
return response()->json($apps);
我得到了这个 json:
{
"current_page": 1,
"data": [
{
"id": "ab221e40-e711-11e8-b124-95a6d9405f4b",
"app_name": "Asf autre",
"country_id": "FR",
"feature_id": "5724ecad-f8d3-3b5f-bd36-d62e97a26798",
"organization_id": "44720e79-eaf9-300a-bbcd-cdfce46dbf54",
"status_id": "PROD",
"created_by": "6d2aeca8-a29d-38b0-bbd7-8f0c9656ca3b",
"updated_by": null,
"created_at": "2018-11-13 07:59:30",
"updated_at": "2018-11-13 07:59:30"
},
{
"id": "f4ccb480-e711-11e8-a6e6-3b8112548306",
"app_name": "Asf pays",
.....
完美。
我想在“数据”键的每个元素中添加一些键。要拥有这种 json:
{
"current_page": 1,
"data": [
{
"akeyhere" : "avaluehere",
"id": "ab221e40-e711-11e8-b124-95a6d9405f4b",
"app_name": "Asf autre",
"country_id": "FR",
"feature_id": "5724ecad-f8d3-3b5f-bd36-d62e97a26798",
"organization_id": "44720e79-eaf9-300a-bbcd-cdfce46dbf54",
"status_id": "PROD",
"created_by": "6d2aeca8-a29d-38b0-bbd7-8f0c9656ca3b",
"updated_by": null,
"created_at": "2018-11-13 07:59:30",
"updated_at": "2018-11-13 07:59:30"
},
{
"akeyhere" : "anothervalue",
"id": "f4ccb480-e711-11e8-a6e6-3b8112548306",
.....
你会怎么做?我认为我必须使用“资源”概念,但我从未使用过,我尝试过但没有成功。
【问题讨论】:
-
在模型上使用
$append变量和访问器 laravel.com/docs/5.7/…(请记住,这仅在您将模型转换为 json 时才有效,原始查询不起作用)
标签: php json laravel pagination