Php 如何向JSON Laravel分页对象添加元素

Php 如何向JSON Laravel分页对象添加元素,php,json,laravel,pagination,Php,Json,Laravel,Pagination,根据这项请求: $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.organiz

根据这项请求:

$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",
       .....
太好了

我想在“data”键的每个元素中添加一些键。要获得这种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",
       .....
你会怎么做?我想我必须使用“resources”这个概念,但我从未使用过,我尝试过,但没有成功。

getCollection()
将返回您的收藏的数据部分。您可以使用
Laravel
采集功能
transform
更新对象

$apps->getCollection()->transform(function($iterator){
  return array_merge($iterator, [
   // add your extra (key, value) here
  ]);
});
注 当您不想每次需要集合项中的额外值时都更新核心模型时,这种方法非常适合

但是

如果有必要,并且您需要此模型中的所有额外属性,则应使用
自定义属性
解决方案。

getCollection()
将返回集合的数据部分。您可以使用
Laravel
采集功能
transform
更新对象

$apps->getCollection()->transform(function($iterator){
  return array_merge($iterator, [
   // add your extra (key, value) here
  ]);
});
注 当您不想每次需要集合项中的额外值时都更新核心模型时,这种方法非常适合

但是


如果这是必要的,并且您需要这些额外的属性,那么您应该使用
自定义属性
解决方案。

您当前正在使用查询生成器来构建查询,如果您想“自动”添加您应该使用的模型类的属性,那么使用查询生成器基本上是相同的

class SomeClass extends Model {
    protected $appends = ['akeyhere'];

    public function getakeyhereAttribute()
    {
        $data = //data from somewhere
        return $data;
    }
}

SomeClass::paginate(10); //will always include `akeyhere`

您还可以使用来更好地处理api输出。

您当前正在使用Query Builder构建查询,如果您想“自动”添加应使用模型类的属性,则与Query Builder基本相同

class SomeClass extends Model {
    protected $appends = ['akeyhere'];

    public function getakeyhereAttribute()
    {
        $data = //data from somewhere
        return $data;
    }
}

SomeClass::paginate(10); //will always include `akeyhere`

您还可以使用来更好地处理api输出。

如果要将对象强制转换为json,可以在模型中使用
$append
变量和如下访问器:

// Get your data using eloquent to retrieve models

$apps = App::orderBy('app_name')->paginate(10);

return response()->json($apps);
在您的模型上添加:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class App extends Model
{
    protected $appends = ['akeyhere'];

    public function getAkeyhereAttribute()
    {
        return 'avaluehere';
    }
}
~生成结果集,然后将其转换为json

$apps = DB::table('apps')->orderBy('app_name')->paginate(10);

$apps = App::hydrate($apps);

return response()->json($apps);
这将把数组的结果集转换为模型,因此您现在可以利用
append
功能

在我看来,你最好利用拉威尔提供的模型功能,所以如果我是你,我会选择第一种


希望这对您有所帮助。

如果要将对象转换为json,可以在模型中使用
$append
变量和如下访问器:

// Get your data using eloquent to retrieve models

$apps = App::orderBy('app_name')->paginate(10);

return response()->json($apps);
在您的模型上添加:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class App extends Model
{
    protected $appends = ['akeyhere'];

    public function getAkeyhereAttribute()
    {
        return 'avaluehere';
    }
}
~生成结果集,然后将其转换为json

$apps = DB::table('apps')->orderBy('app_name')->paginate(10);

$apps = App::hydrate($apps);

return response()->json($apps);
这将把数组的结果集转换为模型,因此您现在可以利用
append
功能

在我看来,你最好利用拉威尔提供的模型功能,所以如果我是你,我会选择第一种

希望这对您有所帮助。


在模型和访问器上使用
$append
变量(请记住,只有将模型转换为json时,此变量才起作用,原始查询不起作用)在模型和访问器上使用
$append
变量(请记住,只有将模型转换为json时,此变量才起作用,原始查询不起作用),这在当前设置中不起作用,我想你应该在你的回答中指出这不适用于OPs当前的设置,我想你应该在你的回答中指出这一点HI Asur,谢谢你的回答。我更喜欢继续使用查询生成器。我理解你所说的迭代。但是如何迭代这个json的“数据”键呢?我试过了,但没有成功。多玛:谢谢你的帮助。你救了我一天。谢谢。我已经更新了我的答案,用了一种更严格的方式修改分页器,看看吧。你好,Asur,谢谢你的回答。我更喜欢继续使用查询生成器。我理解你所说的迭代。但是如何迭代这个json的“数据”键呢?我试过了,但没有成功。多玛:谢谢你的帮助。你救了我一天。谢谢。我已经更新了我的答案,用了一种更简单的方法修改分页器,看看。很高兴它有用。很高兴它有用。