MedicOneSystems / MedicOneSystems/livewire-datatables

Programmatic sorting with external data

Open
#316 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.2k
Forks
257
PR merge metrics
No merged PRs in 30d

Description

Issue: Undefined property: stdClass::$if(JSON_EXTRACT('{"data":[]}',REPLACE(JSON_UNQUOTE(JSON_SEARCH('{"data":[]}',"one",People.person_id,NULL,"$.data[*].employee_id")),"employee_id","proximity")) IS NOT NULL,"yes","no")

[livewire_component.php]
private string $proximity = '{"data":[]}';

public function columns()
{
Column::raw('if(JSON_EXTRACT(''.$this->proximity.'',REPLACE(JSON_UNQUOTE(JSON_SEARCH(''.$this->proximity.'',"one",People.person_id,NULL,"$.data[*].employee_id")),"employee_id","proximity")) IS NOT NULL,"yes","no")')->label('proximity'),
}

I have a page that has two columns. The left column is a livewire-datatable that lists all our employees. The right column is a different livewire component that lists all of our jobs for a given day.

When one of our dispatchers selects a job I need to calculate the distance between the job and the people in the datatable and display that distance in the livewire-datatable. So I used a json array that I populate separately with the unique_id of the person and the proximity. Then I pass that array into the raw sql of the Column. Right now I'm just shooting for yes, no if there is proximity found but I would later would actually display the proximity integer found in the array.

The first time the page loads is fine. But when I select a job from a different livewire component on the page and dispatch the event to the livewire-datatables component which modifies the $this->proximity value. It seems as though the datatable is expecting the exact same raw query back and kicks the above error because the query is different.

My issue is that I need to control the sorting of the table by proximity based on changes to the job selection. It seems as though the sorting functionality is only available off of MySql select columns so I thought a raw query using a php json_array would be appropriate for coorolating this data which doesn't live in the database to the records.

Am I missing a feature that would help me accomplish my sorting goals?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the livewire_component.php example and reproduce the failure by changing $this->proximity after a job-selection event. Inspect how the datatable handles the changing Column::raw query and JSON_EXTRACT expression. Done means the table can update and sort by the externally supplied proximity without the undefined-property error.

Written by the indexing model from the issue text.

Assessment

Tech stack
laravel, php, sql
Domain
backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.