【问题标题】:How to `count` a query result and send it as an array in JSON format?如何对查询结果进行“计数”并将其作为 JSON 格式的数组发送?
【发布时间】:2015-03-07 22:41:55
【问题描述】:

详情

我想要

  • 统计我查询的所有分销商
  • 在 JSON 文件中发送它
  • 我知道我总共有89个经销商,我这样做了dd(count($distributors));

  • 我确信最佳实践是什么。

这是我尝试过的

  • 我初始化$count = 0;
  • 每次循环执行时加 1 $count = $count + 1;
  • 将结果发送到数组'count' => $count->toArray() 作为我的分销商数组的一部分

这是我的代码

public function index()
{
    $distributors = [];
    $count = 0;

    foreach( 

        // Specific Distributors 
        Distributor::where('type','!=','OEM')->where('type','!=','MKP')
        ->get() as $distributor){

        // Variables
        $count = $count + 1; 
        $user = $distributor->user()->first();

        $distributors[$distributor->id] = [

        'user' => $user->toArray(),
        'distributor' => $distributor->toArray(),
        'hq_country' => $distributor->country()->first(),
        'address' => $distributor->addresses()
        ->where('type','=','Hq')->first()->toArray(),

        'count' => $count->toArray()

        ];
    }

    return Response::json($distributors);
}

结果

代码不会运行,因为我的 $distributor 数组不存在...

如果我关闭'count' => $count->toArray(),它将运行。

更新

  • 我使用的是 Laravel 4.0
  • 代码是我的 UrlController.php 的一部分

【问题讨论】:

  • $count = 0;, $count = $count + 1;,那你试试$count->toArray()?您将整数视为对象。
  • @sjagr:你有什么建议来解决这个问题?你能帮我吗
  • 你不能只做'count' => $count吗?不知道为什么你试图将count 变成一个数组,而它只是一个数字。或者(如果你真的想要它作为一个数组)'count => array('count' => $count),但这对我来说似乎是多余的。
  • 我不知道。这段代码不是我写的。这取决于您希望count 对您意味着什么。如果您使用聚合计数,请单独使用'count' => $count。如果您想获取查询的计数,请使用'count' => count($distributors),就像您自己说的那样。
  • 我不知道,但我很难想象您为什么要在结果中发送结果中所有项目的计数。当您收到回复时,您不能只计算它们吗?例如在javascript中:data.length

标签: php arrays json laravel laravel-4


【解决方案1】:

对不起,我可能是错的。

我不是 laravel 专家。

但是这个片段是关于什么的?

这个索引函数是模型的一部分吗?

@evoque2015 正在尝试将一些自定义数组放入 $distributors[$distributor->id]?

如果这是目标,您能否使用您认为应该有效的任何简单的“计数”值进行任何测试?

我的猜测是:'count' 索引对于您的 Distributor::where 函数是不可接受的

(如果可以接受 - 显示为 'count' 的值,即使返回错误的结果/数据也不会破坏您的代码)。

所以我会尝试将此参数的名称更改为“my_custom_count”, 是否应该在 Distributor 模型声明中的某处声明?

找到这个: Add a custom attribute to a Laravel / Eloquent model on load?

这似乎证明了我的猜测。

所以我们需要改变模型类:

class Distributor extends Eloquent {

    protected $table = 'distributors';//or any you have already

    public function toArray()
    {
        $array = parent::toArray();
        $array['count'] = $this->count;
        return $array;
    }

.....
}

可能还有更多的模型变化,或者只是将“计数”列添加到表中:-)

【讨论】:

  • 感谢您的尝试。 ;D
  • 我错了吗?我很确定问题就在那里 - 模型属性声明... :-)
【解决方案2】:

将这种计数添加到您的结果中确实没有多大意义。只发送结果并让客户端进行计数要简单得多。因为有关您实际拥有多少分销商的信息在您的分销商阵列中是正确的。只是它的长度。

这是一个使用 javascript 的示例(和 $.ajax,尽管这并不重要)

$.ajax({
    url: 'get/distributors',
    method: 'GET'
}).done(function(data){
    var count = data.length;
});

【讨论】:

  • 好回答!你介意更新你的答案,并展示如何获得相同的结果,但用 php 代替吗?我正在尝试查看该计数。 - 如果你不介意的话。 :)
  • 什么意思? php 的.lengthcount()。或者我没有正确理解你的问题......
【解决方案3】:

型号:

class Distributor extends Eloquent {
    public function country()
    {
        return $this->hasOne('Country');
    }

    public function addresses()
    {
        return $this->hasMany('Address');
    }

    public function hqAddress()
    {
        return $this->addresses()->where('type', 'Hq')->first();
    }

    public function user()
    {
        return $this->hasOne('User');
    }
}

控制器:

$distributors = Distributor::whereNotIn('type', ['OEM', 'MKP'])
    ->with('country', 'user')->get();

$count = 0;
$distributors->each(function(Distributor $distributor) use(&$count) {
    $distributor->count = $count;
    $count++;
});

return Response::json($distributors);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多