livewire-datatables icon indicating copy to clipboard operation
livewire-datatables copied to clipboard

Withcount Issue

Open abbasmashaddy72 opened this issue 2 years ago • 7 comments

I am using query builder to return the result added all the Question::with('topic')->withCount(['options']);

When I am using the same query with get in Controller, I am getting the options_count result, but I am unable to get the same with query builder

I am getting the column not found error.

Column not found: 1054 Unknown column 'questions.options_count' in 'field list'

Please Help me out

abbasmashaddy72 avatar Mar 18 '22 21:03 abbasmashaddy72

Refer to the Relation example.

First, you have to make a method called getOptionsProperty() where it returns the Options::all(). Then in the column definition, write Column::name('options.id:count'). Complete example:

public function column()
{
    return [
        Column::name('options.id:count')->label('Option count'),
    ];
}

public function getOptionsProperty()
{
    return Option::all();
}

I'm not sure if counting a collection needs the magic property, I'm asking this as well @thyseus.

NikarashiHatsu avatar Mar 25 '22 09:03 NikarashiHatsu

Okay

abbasmashaddy72 avatar Apr 05 '22 09:04 abbasmashaddy72

I am currently having the same issue. I tried the @NikarashiHatsu approach, which is similar to what was suggested on the repository readme file, but I kept getting errors. @abbasmashaddy72 were you able to find a workaround?

dotmarn avatar Nov 17 '22 18:11 dotmarn

What went wrong? I haven't used this package long enough.

Do you have some code to reproduce the issue?

NikarashiHatsu avatar Nov 19 '22 02:11 NikarashiHatsu

@dotmarn

I was not able to find any work around, I simply removed the without from my query as it was not so important at the time of delivery of the Project.

abbasmashaddy72 avatar Nov 19 '22 06:11 abbasmashaddy72

Current Query Builder: public function builder() { return Question::with('topic'); }

Issue Query Builder: public function builder() { return Question::with('topic')->withCount(['options']); }

And Then I was Not Able to get the Count of Options added for the Particular Question

You can have look at my git repo Quiz Repo Link

You can find the same Current Query in app/Http/Livewire/Tables/Admin/QuestionTable.php

And change the query with issue query builder and You can Notice the Issue

@NikarashiHatsu

abbasmashaddy72 avatar Nov 19 '22 06:11 abbasmashaddy72

@dotmarn

I was not able to find any work around, I simply removed the without from my query as it was not so important at the time of delivery of the Project.

I had to leave it too after spending quite some time trying to figure out the issue.

dotmarn avatar Nov 19 '22 13:11 dotmarn