Crud: Still unable to bound multiple times the same column name in v3.3.1

Created on 16 Nov 2017  路  9Comments  路  Source: Laravel-Backpack/CRUD

References: #784 #819

Description:

I got that issue on v3.2.27 and after quick research found out that it is active from 3.2.13 and it is supposed to be fixed in the latest minor version since the code was rewritten and tested.

I have extended CRUD controller which enables AJAX tables and has two columns that are referencing the same table:

        $this->crud->addColumn([
           'label' => "Location",
           'type' => 'select',
           'name' => 'location_id',
           'entity' => 'location',
           'attribute' => 'title',
           'model' => "App\Models\Location"
        ]);
        $this->crud->addColumn([
           'label' => "Location Type",
           'type' => 'radio_location_type',
           'options' => [
               1 => "Country",
               2 => "Region"
           ],
           'name' => 'location_id',
           'entity' => 'location',
           'attribute' => 'type',
           'model' => "App\Models\Location"
        ]);

I tried version 3.2.12 and the result are the expected two columns. Every version after that (including 3.3.1) results in just one column. (I think always the second one)

Bug

Most helpful comment

@shamatov glad to hear it worked out. I just modified the docs to include this hidden feature aswell.

Cheers!

All 9 comments

I encountered the same problem锛孖 overwrite search function in *CrudController and manual generate datatable json data to solve this problem.
i use package " laravel-datatables-oracle" to generate datatable json data.
code like this:

  i suppose main table is user
  public function search(){
         $query = User::selectRaw('user.name,location.title,location.type')
                         ->join('location','user.location_id','=','location.id');

                     return datatables()->eloquent($result)->toJson();
}

if you do like this you should set your custom list view in setup function.
$this->crud->setListView('custom_list');
copy origin list view to custom_list view, just in datatable config option add "columns"

@shamatov , I think we have a yet undocumented feature you could use.

As I understand it, your problem is that you have two columns with the same name. And that name is used in the new AJAX implementation as a key to the $entries property. So only your last column shows up, because it overwrites the first. Right?

If so, you should be able to bypass this by also specifying a different key to one of your columns. Anything you want. It would only be used as a key in the resulted $entries. So for example:

        $this->crud->addColumn([
           'label' => "Location",
           'type' => 'select',
           'name' => 'location_id',
           'entity' => 'location',
           'attribute' => 'title',
           'model' => "App\Models\Location"
        ]);

        $this->crud->addColumn([
           'label' => "Location Type",
           'type' => 'radio_location_type',
           'options' => [
               1 => "Country",
               2 => "Region"
           ],
           'name' => 'location_id',
+          'key' => 'location_type',
           'entity' => 'location',
           'attribute' => 'type',
           'model' => "App\Models\Location"
        ]);

This should fix your problem, right? Can you please confirm?

i was having this same issue.. and tried this key method.. but didnt work.
but im on crud 3.1 version

Hi @tabacitu

It worked for me on v3.3.1

Thanks a lot!

@mpixelz the key method will only work on 3.3, sorry.

@shamatov glad to hear it worked out. I just modified the docs to include this hidden feature aswell.

Cheers!

It worked for me too on 3.3.3 but not with addColumns, just addColumn. In fact, addColumns has some issues when trying to display data from a related table (it treats ever member of the column definition as a column heading). Do you need a proper bug report for this or do you already have it tracked?

@tedsecretsource what do you mean by "_it treats ever member of the column definition as a column heading_"? Calling addColumns(['smth', 'smth_else']) should be the same as running addColumn('smth') and addColumn('smth_else'), that's what addColumns() does.

Yup, if you think you found a bug with addColumn() let's open another issue please and not polute this one. It sounds like a different bug to me.

Thanks, cheers!

Took me a day to find this solution in FAQ. Would be great for others if it's noted directly at the doc section instead of the FAQ. As tables commonly display multiple column attibutes from the same relationship. Such as displaying the name and email of a product's user.

Also, I have read this was confirmed to work on 3.3.1, but on my 3.3.6, it doesn't. Until I found the solution in FAQ. Still, cheers for the solution!

Was this page helpful?
0 / 5 - 0 ratings