So I have the following scope on an Investor Model:
public function scopeCountUsersInRegionForCompany(Builder $query, $companyName, $regionCode)
{
return $query->select('users.*')
->join('user_profile', 'user_profile.user_id', '=', 'users.id')
->join('investments', 'users.id', '=', 'investments.user_id')
->join('offerings', 'investments.offering_id', '=', 'offerings.id')
->join('companies', 'offerings.company_id', '=', 'companies.id')
->where('companies.slug', '=', $companyName)
->where('user_profile.region', '=', $regionCode)
->count();
}
Its used in the following context:
public function regionCountForCompany($slug)
{
$regions = [];
$totalUsersForCompany = Investor::countUsersForCompany($slug);
if ($totalUsersForCompany === 0) {
return [];
}
foreach ($this->getAllRegions() as $regionCode => $regionName) {
$regions[$regionName] = (Investor::countUsersInRegionForCompany($slug, $regionCode) / Investor::countUsersForCompany($slug)) * 100;
}
if (max($regions) > 0) {
return $regions;
} else {
return [];
}
}
Note the for each loop:
foreach ($this->getAllRegions() as $regionCode => $regionName) {
$regions[$regionName] = (Investor::countUsersInRegionForCompany($slug, $regionCode) / Investor::countUsersForCompany($slug)) * 100;
}
the following call:
Investor::countUsersInRegionForCompany($slug, $regionCode)
will return 1 and then every other time after that it returns: Object of class Illuminate\Database\Eloquent\Builder could not be converted to int If I do: var_dump(is_object(Investor::countUsersInRegionForCompany($slug, $regionCode))) Its false once, then true for every other time its called.
This is the only scope that causes this issue. How ever if I do a var dump inside the scope to get the variable $query instead of returning it like you see me doing, it comes back as an int.
If I hit the end point in the browser that calls this particular set of code, all of this works, no errors about objects or any of that. The test is as follows:
public function it_should_return_region_count_for_investors() {
$this->createInvestment();
$investor = factory(User::class)->create([
'role_id' => 4
]);
$response = $this->actingAs($investor)
->call('GET', 'api/v1/investors-signedup-by-jurisdiction/');
$this->assertNotEmpty(json_decode($response->getContent()));
}
The response comes back as: Object of class Illuminate\Database\Eloquent\Builder could not be converted to int (It actually renders a html laravel error page with this message)
What is going on?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire