Hi,
This is my crud config:
$this->crud->addColumn([
'name' => 'empresa_id',
'label' => 'CUIT',
'type' => 'select',
'entity' => 'empresa', // the method that defines the relationship in your Model
'attribute' => 'cuit', /**/ foreign key attribute that is shown to user
'model' => 'App\Models\Empresa', // foreign key model
]);
$this->crud->addColumn([
'name' => 'empresa_id',
'label' => 'Empresa',
'type' => 'select',
'entity' => 'empresa', // the method that defines the relationship in your Model
'attribute' => 'razonsocial', // foreign key attribute that is shown to user
'model' => 'App\Models\Empresa', // foreign key model
]);
It was working fine, i was able to show 2 additional columns with the same attribute 'name' attribute to get 2 different fields ('cuit' and 'razonsocial') from the same model 'Empresa'.
Something in last version [3.2.13] - 2017-07-07] breaked this behavior.
Now, the crud only keeps the lastest if i repeat the column name attribute.
I guess that i can solve by using a different crud only for that list (i don't need this for edition) but i need to know if there are any way to have the result columns as before.
Thanks, i appreciate your effort to make this amazing packages.
I believe it's a duplicate of #784.
@tabacitu fixed it in 0bf9494f6ca3ce5bea98887aa6f76bf24c187494 on dev branch. But the fix was only for non-ajax tables (#819).
Duplicate of #784.
For anybody who ends up here with the same problem: a hidden feature has been introduced since this issue has been posted. You can now use the key attribute on a column to specify a different key for the array it's stored in. So you can use two columns with the same name, as long as you specify a different key for one of them. Eg:
$this->crud->addColumn([
'name' => 'empresa_id',
+ 'key' => 'cuit',
'label' => 'CUIT',
'type' => 'select',
'entity' => 'empresa', // the method that defines the relationship in your Model
'attribute' => 'cuit', /**/ foreign key attribute that is shown to user
'model' => 'App\Models\Empresa', // foreign key model
]);
$this->crud->addColumn([
'name' => 'empresa_id',
+ 'key' => 'razonsocial',
'label' => 'Empresa',
'type' => 'select',
'entity' => 'empresa', // the method that defines the relationship in your Model
'attribute' => 'razonsocial', // foreign key attribute that is shown to user
'model' => 'App\Models\Empresa', // foreign key model
]);
Hope it helps someone.
Cheers!
Fantastic - thanks for that @tabacitu - that fixed my issue.
Glad to help @bc-moe !
Is there a similar thing for fields where I can use the same db column?
@aminsadiq350 no there isn't, unfortunately. That's because the name will also be the name of the HTML input. And inside an HTML form, if you have more inputs with the same name, only the last input will be taken into consideration.
The best solution that I can think of is to use a Laravel accessor and mutator on the Model. Then you can use that in Backpack just like it were a regular column in the database. Especially if you add it to $appends on the model.
Hope it helps!
@tabacitu Thanks for the heads up!
Could you please check out my post on stackoverflow: https://stackoverflow.com/questions/62728683/backpack-for-laravel-select2-multiple-field-with-same-pivot-table-but-different
and guide me through how to go about it cause am getting confused with getting the relationship to work with accessors/mutators since the field i want to catch while form submit doesn't exist on the table am working with, not sure how to go about it.
P.S am new to laravel and backpack
@tabacitu Thanks for a quick response. As suggested on stackoverflow - I tried and ended up with an error saying "this attribute does not exist on this collection instance" which i was expecting...
Ok so am trying a different approach by creating my own field using a custom view which i load using the view field in backpack. Now how do I get that to store data when I click on the save button on the form, cause am not really sure where and how the form is being submitted and where will I be able to handle my http request.
Most helpful comment
For anybody who ends up here with the same problem: a hidden feature has been introduced since this issue has been posted. You can now use the
keyattribute on a column to specify a different key for the array it's stored in. So you can use two columns with the same name, as long as you specify a different key for one of them. Eg:Hope it helps someone.
Cheers!