Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
oh actually fucking raw sql gives 51 grouped rows with count 1
so thats why it is
select count(*)
from `companies`
left join `company_contact` on `company_contact`.`company_id` = `companies`.`id`
group by `companies`.`id`
order by `name` asc
so only subquery and count(*) will give me correct count? -
Uff.
?? instead of isset
DTO instead of arrays
Why do you not just count the companies? You're already fetching all results, so the necessity of an SQL query isn't given. -
Lyniven44533yWhat an horrible way to use eloquent (I guess it's laravel)
Also the guy above is right -
C0D4669023yI will never understand why people build queries like this.
It's an unmaintainable, illegible mess. -
nitnip18133yThis is a server-side query for that abominable jquery datatables library isn't it? I recognize that asinine way of tracking filters.
-
john-doe9363yI see this and the first thought that comes to my head is.
Why doesn't devrant support markdown???? -
nitnip18133y@galileopy Probably to avoid people using devrant as a discount 'fix-my-code' stackoverflow. It's not like it's stopping some third-world country companies from using the platform to post job advertisements.
...could also just be laziness. -
@IntrusionCM phpstorm suggested ?? instead of isset but I disabled this suggestion, for me isset is more readable. No need to think what isset means. Everytime I read ??, I need to think what does it mean or even google.
-
@IntrusionCM why DTO instead of arrays? you mean create new class? Too much work I think.
-
@IntrusionCM I am not fetching all companies, I am only fetching one page of companies.
-
@Lyniven what I am only thinking to refactor is to move eloquent code to repository class.
-
@nitnip I think your query count() call would only return count of items in one page. But interesting syntax ->when() , havent seen such.
-
@galileopy code highlighting really would help. And I see at the same time you can rant and get help, not like on stackoverflow - they downvote if they think its a rant, so this would be very beneficial.
How to contact devrant.com owners to suggest this feature? I can't see contacts.
Related Rants
public function index(Request $request): array
{
$parameters = json_decode($request->get('lazyEvent'), true);
if (isset($parameters['page'])) {
$page = $parameters['page'];
} else {
$page = 0;
}
$queryBuilder = DB::table('companies')
->leftJoin('company_contact', 'company_contact.company_id', '=', 'companies.id')
->groupBy('companies.id')
;
if (isset($parameters['filters']['name'])) {
$queryBuilder
->where('name', 'like', '%' . $parameters['filters']['name']['value'] . '%')
;
}
$total = $queryBuilder
->count()
;
$companies = $queryBuilder
->select('companies.id', 'name', 'email', 'phone', DB::raw("COUNT('company_contact.id') AS contact_count"))
->orderBy('name')
->offset($page * $parameters['rows'])
->limit($parameters['rows'])
->get();
return ['companies' => $companies, 'totalRecords' => $total];
}
what is this shit? I get $total 1 when in reality is $companies count is 51
I am thinking avout writing whole sql as raw because I cannto get fucking count correctly
rant
laravel