Reputation:
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;
}
}
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?
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);
}
}
$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!
}
Upvotes: 2
Views: 1851
Reputation: 1559
try instead of:
return $bounceZoneList;
use:
return collect($bounceZoneList);
Upvotes: 0