Making a withCount within another relationship that is within a relationship

Asked

Viewed 24 times

1

I have 3 tables, Client, Budget and Ordemservicos.

$clientes = Cliente::withCount('orcamento')->orderBy('orcamento_count', 'desc')->get();

the code above returns me what I need, but I need the following

need to do a withCount in the table "ordemservico" however there is inside the table of "orcamento" that is related.

needed something like for example...

$clientes = Cliente::withCount(['orcamento','ordemservico'])->orderBy('ordemservico_count', 'desc')->get();

any idea ?

  • I think it would look something like this:;$clientes = Cliente::withCount(['orcamento' => function (Builder $query) {
 $query->withCount(['ordemservico']);
}])->orderBy('orcamento_count', 'desc')->get();


  • tested gave this mistake bro.. " Argument 1 passed to App Repositories Clientesrepository::App Repositories{closure}() must be an instance of App Repositories Builder, instance of Illuminate D"

  • i tried doing so... "$clients = Client::withCount('orcamento','orcamento.ordemservico')->orderby('orcamento_count', 'desc')->get();" I think that would be the correct way is not the right way ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.