2

I'm new to Laravel.

I use laravel 5.4.

I want to make my query scope method in a model class return an array object.

But, it seems that it returns query builder object instead of array object.

How can I return an array instead of query builder object in a query scope method?

<?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;

    }


}

edit

I don't want to have the code above in my controller class. My controller class gets fat, so I want to move it to a related model class. Any way to accomplish it?

edit2

How come it doesn't add a value to the array in side of each loop??

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);

}

}

EDIT 3

$collection = $query->get();

var_dump($collection->groupBy("bounce_zone")->toArray());

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!
    
}
5
  • Have you tried return $bounceZoneList->toArray();? Commented May 15, 2017 at 7:45
  • Why do you want an array returned instead of the query to execute? Query scopes are about modifying the query before you execute it, not about returning data Commented May 15, 2017 at 7:46
  • @Antonis Tsimourtos it doesn't work. Commented May 15, 2017 at 7:48
  • @Mark Baker I see... But I don't want to have the code above in my controller class. My controller class gets fat, so I want to move it to my model class. Any way to accomplish it? Commented May 15, 2017 at 7:50
  • 1
    Mutators look like a better fit to what you need. Commented May 15, 2017 at 7:51

1 Answer 1

0

try instead of:

return $bounceZoneList;

use:

return collect($bounceZoneList);
Sign up to request clarification or add additional context in comments.

11 Comments

It's working! After returning array as collect object, to a controller class, I can change it to an array object :)
Do you know why it doesn't add a value to the array in side of each loop? I added the code on my post.
what's return either of $query->get() and $collection->groupBy("bounce_zone")->toArray()?
I added the var_dump log when doing $collection->groupBy("bounce_zone")->toArray().
instead of $bounceZoneList[] = 1; try $bounceZoneList[] = ['a' => 'something']; and see the result
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.