2

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

Comments
  • 0
    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?
  • 0
    yes with damn subquery I made it work.
  • 0
    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.
  • 0
    What an horrible way to use eloquent (I guess it's laravel)

    Also the guy above is right
  • 0
    Ugh, uninvited code right in my face.
    Can’t you see I’m on vacation? ☹️
  • 1
    I will never understand why people build queries like this.

    It's an unmaintainable, illegible mess.
  • 0
    This is a server-side query for that abominable jquery datatables library isn't it? I recognize that asinine way of tracking filters.
  • 0
    I was bored. Here's a more SQL-like syntax.

    https://pastebin.com/CikaCgHM
  • 0
    Is this your brain on PHP?
  • 1
    I see this and the first thought that comes to my head is.

    Why doesn't devrant support markdown????
  • 0
    @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.
  • 0
    @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.
  • 0
    @IntrusionCM why DTO instead of arrays? you mean create new class? Too much work I think.
  • 0
    @IntrusionCM I am not fetching all companies, I am only fetching one page of companies.
  • 0
    @Lyniven can you suggest something better beside what guy above suggested?
  • 0
    @Lyniven what I am only thinking to refactor is to move eloquent code to repository class.
  • 0
    @C0D4 how do you suggest to build?
  • 0
  • 0
    @nitnip I think your query count() call would only return count of items in one page. But interesting syntax ->when() , havent seen such.
  • 0
    @SuaveSteve it is my brain who created this code :)
  • 0
    @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.
Add Comment