如何在模型类的查询范围方法中返回数组对象

时间:2017-05-15 07:43:27

标签: php laravel laravel-5

我是Laravel的新手。

我使用了laravel 5.4。

我想让我的查询范围方法在模型类中返回一个数组对象。

但是,似乎它返回查询构建器对象而不是数组对象。

如何在查询范围方法中返回数组而不是查询构建器对象?

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;

/**
 * @method static VisitRecord create(array $attributes)

 */

class VisitRecord extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];


    public function scopeBounceZone($query) {

        $collection = $query->get();

        $bounceZoneList = [];

        $collection->groupBy("bounce_zone")->each(function($group, $key) {

            if ($group[0]["bounce_zone"] === 0 ) {

                $bounceZoneList["goal"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 1 ) {

                $bounceZoneList["knock"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 2 ) {

                $bounceZoneList["approach"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 3 ) {

                $bounceZoneList["front"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 4 ) {

                $bounceZoneList["detail"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 5 ) {

                $bounceZoneList["closing"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 6 ) {

                $bounceZoneList["hook"] = count($group);

            }

            if ($group[0]["bounce_zone"] === 7 ) {

                $bounceZoneList["finish"] = count($group);

            }


        });


        return $bounceZoneList;

    }


}

修改

我不想在我的控制器类中使用上面的代码。我的控制器类变胖,所以我想将它移动到相关的模型类。有什么办法可以实现吗?

EDIT2

为什么不在每个循环的一侧向数组添加值?

public function scopeBounceZone($query) {

    $collection = $query->get();

    $bounceZoneList = [];

    $collection->groupBy("bounce_zone")->each(function($group, $key) {

        echo "it's called ok";

        // just a test.
        $bounceZoneList[] = 1;



    });

    //array(0) { } array(0) { }
    var_dump($bounceZoneList);


    return collect($bounceZoneList);

}

}

编辑3

$ collection = $ query-&gt; get();

的var_dump($收藏 - &GT; GROUPBY( “bounce_zone”) - &GT;指定者());

array(12) {
  [0]=>
  array(13) {
    ["id"]=>
    int(26)
    ["room_id"]=>
    int(14)
    ["project_id"]=>
    int(1)
    ["bounce_zone"]=>
    int(0)
    ["bounce_reason"]=>
    int(1)
    ["next_action"]=>
    int(1)
    ["memo"]=>
    string(0) ""
    ["staff_id"]=>
    int(1)
    ["visited_at"]=>
    string(19) "2017-05-15 00:00:00"
    ["weather"]=>
    string(5) "snowy"
    ["created_at"]=>
    string(19) "2017-05-15 15:02:35"
    ["updated_at"]=>
    string(19) "2017-05-15 15:02:35"
    ["deleted_at"]=>
    NULL
  }
  [1]=>
  array(13) {
    ["id"]=>
    int(51)
    ["room_id"]=>
    int(10)
    ["project_id"]=>
    int(1)
    ["bounce_zone"]=>
    int(0)
    ["bounce_reason"]=>
    int(0)
    ["next_action"]=>
    int(2)
    ["memo"]=>
    string(0) ""
    ["staff_id"]=>
    int(1)
    ["visited_at"]=>
    string(19) "2017-05-15 00:00:00"
    ["weather"]=>
    string(5) "sunny"
    ["created_at"]=>
    string(19) "2017-05-15 15:02:35"
    ["updated_at"]=>
    string(19) "2017-05-15 15:02:35"
    ["deleted_at"]=>
    NULL
  }

  and more!

}

1 个答案:

答案 0 :(得分:0)

尝试而不是:

return $bounceZoneList;

使用:

return collect($bounceZoneList);