Crud: ID changed after apply filter with several join table

Created on 27 Nov 2017  路  3Comments  路  Source: Laravel-Backpack/CRUD

Hello,

I have a little issue with the filter option. I have sereval tables in my database which are linked by primary key.
It does something like this :

Site -> Parcel (has site_id) -> Tree (has parcel_id)

In my code, in TreeCrudCrontroller.php I do something like this :

   $this->crud->addFilter([
        'type' => 'text',
        'name' => 'site',
        'label' => 'Site'
    ],
        false,
        function ($value) {
            $this->crud->addClause('join', 'parcels', 'parcel_id', 'parcels.id');
            $this->crud->addClause('join', 'sites', 'parcels.site_id', 'sites.id');
            $this->crud->addClause('where', 'sites.name', 'LIKE', "%$value%");
        }
    );`

    $this->crud->addFilter([
        'type' => 'text',
        'name' => 'owner',
        'label' => 'Propri茅taire'
    ],
        false,
        function ($value) {
            $this->crud->addClause('join', 'parcels', 'parcel_id', 'parcels.id');
            $this->crud->addClause('join', 'sites', 'parcels.site_id', 'sites.id');
            $this->crud->addClause('join', 'owners', 'sites.owner_id', 'owners.id');
            $this->crud->addClause('where', 'owners.first_name', 'LIKE', "%$value%");
        }
    );

In my Tree model, I have this code :

   public function parcel()
   {
    return $this->belongsTo(Parcel::class);
   }

  public function displaySite()
  {
    $site = Tree::with('parcel.site')->find($this->id);
    return $site->parcel->site->name;
  }

  public function displayOwner()
  {
    $owner = Tree::with('parcel.site.owner')->find($this->id);
    return $owner->parcel->site->owner->full_name;
  }

The problem is that when I show my table for the first time, without filters, both Site and Owner display the right text BUT when I filter on one of them, the ID of the current Tree is changing.

If I use to filter by Site, the Tree ID is changed by the Site ID
If I use to filter by Parcel, the Tree ID is changed by the Parcel ID

I don't know if I do something wrong or if it's a bug.

I'm on the lastest version of backpack/base and backpack/crud.

I hope that I'm as understandable as possible :)
Thanks for your help 馃憤

Ask-It-On-Stack-Overflow question

Most helpful comment

Found it in the Laravel doc
$this->crud->addClause('select', 'my_table.*');

Thanks for you help 馃憤

All 3 comments

This is almost certainly data tables + laravel playing up; essentially with relations present, if select("the_base_table.*") wasn't the last thing called, the sorting would do what you're saying.

I cannot for the life of me find the URL where I found that.

Yeah, you're right.

After reading your message, I tried by myself in my PhpMyAdmin with a select * and a select my_table.*

The result without the my_table.* return this:
image

I didn't figured out before.

So Laravel just keep the last ID column. I just need to change the select.

But now I need to know how 8-)..

Found it in the Laravel doc
$this->crud->addClause('select', 'my_table.*');

Thanks for you help 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

M0H3N picture M0H3N  路  3Comments

abewartech picture abewartech  路  3Comments

AlexanderWM picture AlexanderWM  路  3Comments

mikael1000 picture mikael1000  路  3Comments

bahman2216 picture bahman2216  路  3Comments