Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Amaro's avatar
Level 1

Eloquent, Query Builder or other way?

Eloquent, Query Builder or other way?

Hi to all! I want to present a tablet with clients and the “number” of related records, in this case number of orders from CONTACTS table. In php I had can resolve this in two ways!

FIRST WAY Make a simple join select with count like this: SELECT clientes.*, Count(clientes_contactos.cliente_id) AS ncontactos FROM clientes LEFT JOIN clientes_contactos ON clientes_contactos.cliente_id = clientes.clienteid GROUP BY clientes.clienteid

SECOND WAY include a php with the query inside just getting the records for that $id.

In Laravel I can transform the eloquent to “count” trough query builder that and the result from FRIST option, don’t’ have the smallest idea how to get the result from second way (above) in Laravel.

Right now getting results with: $dados = clientes::all(); return view('clientes.listar', compact('dados'));

In image below present the count number in place of the RED circle. https://1drv.ms/u/s!Amqa9MXG8tF5mIoHo9BULDIWJB0dyQ

What would be the best approach for this case and similar? Thanks in Advance! Amaro

0 likes
12 replies
D9705996's avatar

You could do

Clientes::with('clientes_contactos')
    ->addSelect(DB::raw('count(*) as total'))
    ->groupBy('clienteid')
    ->get();

I've not tested but should work (you might need to tinker a bit).

Cronix's avatar

Do you want to actually get the related items, or only a count of the related items?

If it's just the count without the contents

$clients = Clientes::withCount('clientes_contactos')->get();

then in the view

@foreach ($clients as $client)
  Client Name: {{ $client->name }}
  Contact Count: {{ $client->clientes_contactos_count }}
@endforeach
D9705996's avatar

I don't think withCount will help you as it eager loads the related table count so wouldn't take into consideration the group by.

Amaro's avatar
Level 1

Ty @Cronix ! The count number will sufice. But when I use the $clients = Clientes::withCount('clientes_contactos')->get(); it gives me the error: Call to undefined method App\clientes::clientes_contactos(). Do I have to define this on the Clientes model ?

Amaro's avatar
Level 1

@cronix Thank you! I guess that is my struggle. The "creation of relationships" will give it a go and post a feedback asap. This is a new concept for me.

Amaro's avatar
Level 1

@Cronix

It is working did this (for future reference or in case another person need this info). In the Client model defined relation to contacts and other away arround also hasMany - belongsTo. Called within controller with GET all good and working. Thank you!

Now for the next level.... what if, we need to call the count of other relation still lets say... Number of contacts (already done) and number of orders from orders table. (assuming the relationship is already done)

Will watch the videos thanks, started with laravel 3 days ago!

Cronix's avatar

what if, we need to call the count of other relation still lets say... Number of contacts (already done) and number of orders from orders table. (assuming the relationship is already done)

The exact same way, except a separate withCount() method.

$clients = Clientes::withCount('clientes_contactos')->withCount('orders')->get();
@foreach ($clients as $client)
  Client Name: {{ $client->name }}
  Contact Count: {{ $client->clientes_contactos_count }}
  Order Count: {{ $client->orders_count }}
@endforeach

assuming the name of the relationship is orders and the relationship is defined in the Clientes model.

If you have more questions, please ask in a new thread. It's best to have 1 question/answer per thread for others looking for similar answers.

1 like
Amaro's avatar
Level 1

It try it latter! Amazing this "laravel" thingy :) awsome... really nice framework! Thank you @Cronix

Please or to participate in this conversation.