orchidsoftware / crud

Simplify the process of building CRUD (Create, Read, Update, Delete) functionality in Laravel using the features of Orchid.
https://orchid.software
MIT License
138 stars 34 forks source link

Filter causes ID to display wrongly #32

Closed bilogic closed 3 years ago

bilogic commented 3 years ago

image

  1. I have 2 tables contacts and payments, related by payments.contact_id
  2. I wrote a QueryFilter.php for payments filtering by using contacts information

    public function run(Builder $builder): Builder
    {
        $searchTerm = $this->request->get('search');
    
        return $builder
            ->join('contacts', 'contacts.id', '=', 'payments.contact_id')
            ->where('contacts.surname', 'LIKE', '%' . $searchTerm . '%')
            ->orWhere('contacts.email', 'LIKE', '%' . $searchTerm . '%')
        ;
    }
  3. In my Payments.php resource, I had to do this:

    public function columns(): array
    {
        return [
            TD::make('id'),
        ...
    }
    
    public function filters(): array
    {
        return [
            \App\Orchid\Filters\PaymentsFilter::class,
            new DefaultSorted('payments.id', 'desc'),
        ];
    }
  4. Before filter is applied, both payments.id and contacts.id shows the correct values
  5. After filter is applied, payments.id shows the value of contacts.id
  6. Looks like a bug to me, any idea?

Thank you.

tabuna commented 3 years ago

Hi, I am using links, and I have no such problem. Maybe join is affecting. It would be great to see the result of what is returned in its purest form.

Try to write those in the same way as I do:

return $builder->whereHas('contacts', function (Builder $query) {
    $searchTerm = $this->request->get('search');

    $query
        ->where('surname', 'LIKE', '%' . $searchTerm . '%')
        ->orWhere('email', 'LIKE', '%' . $searchTerm . '%');
});
bilogic commented 3 years ago

Yes, thank you! Yours worked! Strange, what did I do wrong?

bilogic commented 3 years ago

@tabuna sorry to sidetrack a bit, is there a way to apply QueryFilter.php to a column instead of the table?