Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.08k stars 887 forks source link

select2_from_ajax does not show the field value when update ( with entity => false ) #5366

Closed miquelangeld closed 4 months ago

miquelangeld commented 10 months ago

I'm using this field type

$this->crud->addField([ 'label' => 'Booking', 'type' => 'select2_from_ajax', 'name' => 'booking', 'entity' => false, 'attribute' => 'booking', 'model' => "App\Models\PayByLink", 'data_source' => url('/admin/bookings/ajax-bookings-avalon-options'), ]); when I create a new entry it works, the info is stored in database, but when I edit the entry the field shows empty. I'm using entity False because this field is not a relationship, just a text field. I use the same solution in many places in that project and I have the same problem, if i set entity to false the field shows empty

My workaround for now is to show the field as a regular text field in the update view in order to show the stored value, but I would like to use the ajax search in the update view too.

PHP VERSION:

PHP 8.1.12-1ubuntu4.3 (cli) (built: Aug 17 2023 17:37:48) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.12, Copyright (c) Zend Technologies with Zend OPcache v8.1.12-1ubuntu4.3, Copyright (c), by Zend Technologies

LARAVEL VERSION:

10.30.1.0

BACKPACK PACKAGE VERSIONS:

backpack/backupmanager: v5.0.0 backpack/basset: 1.2.1 backpack/crud: 6.3.0 backpack/devtools: 2.0.3 backpack/editable-columns: 3.0.2 backpack/generators: v4.0.2 backpack/logmanager: v5.0.1 backpack/permissionmanager: 7.1.1 backpack/pro: 2.0.18 backpack/settings: 3.1.0 backpack/theme-coreuiv2: 1.2.2 backpack/theme-coreuiv4: 1.1.1 backpack/theme-tabler: 1.1.1

tabacitu commented 10 months ago

Hi @miquelangeld ,

Huh... turns out this is a known issue (see https://github.com/Laravel-Backpack/CRUD/issues/3195 ), don't know why we haven't fixed this yet 🤷‍♂️ Sorry for that.

TLDR:

Possible solutions:

What do you think @pxpm ? Any better solution? Which one do you think we should do?

miquelangeld commented 10 months ago

Hi @tabacitu In this project in all cases there is a linked model which I need to feed up with external data (sometimes an external api or sometimes an endpoint within the same project but from external databases, in fact the data is inserted correctly for the model PayByLink in the create view. the problem comes when I want to update and the field doesn't shows the stored data.

If I may, the idea of a select2_from_api field type may be a solution less disruptive for you, the need to query external data to fill a model field it's very present in all my projects.

tabacitu commented 10 months ago

Hmm @miquelangeld then maybe it's how you've defined that Model... sounds like it doesn't get picked up as a "normal" Eloquent model. What did you use for your "fake" models, did you use https://laravel-news.com/laravel-sushi ? I've used it a bunch of times and it's GREAT for creating Fake models that actually get their data from an array/file/api/etc.

miquelangeld commented 10 months ago

Hi @tabacitu Let me give you more details, maybe I'm doing something wrong or maybe I'm not explaining the issue correctly.

It's not a fake model. I have a Laravel model named PayByLink, one of the fields is a string field named 'booking'. The users needs to search among thousands of booking codes from another system the one they need (that's why I need ajax), but there aren't any relationship, I just want to store the value from the endpoint (That's why i set entity to false)

The CRUD Field is defined like this:

$this->crud->addField([ 'label' => 'Booking', 'type' => 'select2_from_ajax', 'name' => 'booking', 'entity' => false, 'attribute' => 'booking', 'model' => "App\Models\PayByLink", 'data_source' => url('/admin/bookings/ajax-bookings-avalon-options'), ]);

the endpoint return for example this:

[{"id":"MURR23000001-1","booking":"MURR23000001-1 Localizador: 40737RT33"},{"id":"MURR23000002-1","booking":"MURR23000002-1 Localizador: 4073769XC"},{"id":"MURR23000003-1","booking":"MURR23000003-1 Localizador: SHC24_025"},{"id":"MURR23000004-1","booking":"MURR23000004-1 Localizador: OTH7453390"},{"id":"MURR23000005-1","booking":"MURR230044004-1 Localizador: OTB345691"}]

In the dropdown options shows the value of 'booking' and in database is stored the value of 'id'

so, when the user pick the option "MURR230044000-1 Localizador: 40737RT33" In database the data stored is 'MURR23000001-1' So far so good.

But if I try to edit any PayByLink entry, the field not shows the current value, just the empty dropdown.

miquelangeld commented 10 months ago

Hi @tabacitu I don't know if you have had a chance to take a look at my last update. Thanks in advance

miquelangeld commented 9 months ago

Hi, any chance to get some news about this? Thanks!

pxpm commented 9 months ago

@miquelangeld this is a technical limitation of the select2_from_ajax. Sorry, you are not doing nothing wrong.😞

We plan to create select2_from_api to avoid messing (with the already messy) select2 from ajax code that should just fit in your use case.

Can't promisse a date, but it's on our January roadmap if everything goes accordingly.

image

Cheers

miquelangeld commented 9 months ago

Thanks @pxpm Great!

miquelangeld commented 6 months ago

Hi @pxpm any news about this feature? Thanks in advance

miquelangeld commented 4 months ago

I close the issue since select2_json_from_api is released!!