简体   繁体   中英

Laravel Backpack - ID changed after apply filter with several join table

I'm using Laravel Backpack to create a proper back office.

But 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 Laravel, backpack/base and backpack/crud.

I hope that I'm as understandable as possible :)

Thanks for your help 👍

Edit : When I try the SQL request in my PMA, I have something like this : SQL result

So I think Laravel just keep the last ID column despite of the first ID column

In fact, just need to add a select close.

$this->crud->addClause('select', 'my_table.*');

Seems weird but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM