search problem when joining multiple tables
Describe the bug


when i search with field "fb.name" how to declare in function columns
Laravel version (exactly):
- 8.65
Alpine version:
- "power-components/livewire-powergrid": "^1.4"
Thank for help me
In my case I have two tables human_resources and centres, where one HR belongs to one centre, so I need to search on centre name which comes from centres table, so I used below modification to make it work:
return HumanResource::query()
->join(DB::raw('
(SELECT id as centre_id, name as centre_name, alpha_code as centre_alpha_code, parent_id
FROM centres) as centre
'), function ($join) {
$join->on('human_resources.center_id', '=', 'centre.centre_id');
})
->select('human_resources.*','centre.centre_name');
And then I add below line in columns function
Column::add()->title('Centre')->field('centre_name')->makeInputText()
See also #111 Not resolved although @luanfreitasdev believes it is.
These are different problems, #111 says solve the sortable problem while this one is about advanced search with joins.
Maybe you'll need to put a $table variable inside the searchable so you don't get lost.
@luanfreitasdev Its a problem because you reverted the previous table prefix. There is still a problem with sorting also, so for now, I abandoned using the project
Prefix doesn't cause problem with join ?
I uploaded some changes directly to main, which works with a prefix with the table name. The problem with the previous prefix is that in the back state when sortable is clicked, it would no longer recognize it, and the state started like this:
$table = 'dishes';
I clicked on sortable ('categories.name')
The component was understand like this:
$table = 'categories.name' (but categories is not the main one in the component).
So to solve this, I removed the round trip data from the component when the table name is the same, allowing no more table not found.
Prefix resolves table duplication.
This change does not fix advanced filters, only component initialization and ordering
Remember that this project is not funded but open-source. Everyone can collaborate with him to make him better in every way.,
I just upload advanced search support with join in inputText, dataPicker and boolean filter.
Example that worked (both in searches and sorting):
Column::add()
->title(__('Dish'))
->field('dishes.name')
->searchable()
->editOnClick($canEdit)
->makeInputText('name')
->sortable('dishes.name'),
Column::add()
->title(__('Created Categories'))
->sortable('categories.created_at')
->field('created_at_formatted')
->makeInputDatePicker('categories.created_at'),
I'm waiting for confirmation if it worked @snapey , @qhanm
Hi, I made some more improvements and I just uploaded improvements.
it doesn't work, when i search by "fb.name as name" field the sql sentence is rendered incorrect, the correct query should be "fb.name like %test%"
@luanfreitasdev
Did you just install the last commit from main ?
field('name') has to be field('fb.name') to work
yes i just installed the latest livewire-powergrid
dev-main ? You also have to fix the field('fb.name')
There's no way your code is with the dev-main, as my last commit prefixes the column.
You need to install the package by passing the dev-main
I think it's because in the query count total record the field name is not found due to "select count(*) aggre"
You need to do two things;
- composer require power-compoments/livewire-powergrid dev-main
- Change the field('name') method to field('fb.name')
That done. The name column will become fb.name
oh sorry, since i haven't cleared cache composer, i just updated to the latest and it worked. thanks for your help My problem has been resolved so I'm closing this discussion Thanks.
You need to do two things;
- composer require power-compoments/livewire-powergrid dev-main
- Change the field('name') method to field('fb.name')
That done. The name column will become fb.name
Thanks