Laravel-Backpack / CRUD

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

[Bug] inlineCreate not working on hasMany relationship #5703

Open mrinaldidfs opened 1 month ago

mrinaldidfs commented 1 month ago

Bug report

I have added the "inline create" function to BelongsTo relationships several times and have never had any problems. I tried adding that function in a field with the hasMany relationship and the add inline button doesn't appear

What I did

In the main Model (Organization) I have the contacts relationship

public function contacts(){
     return $this->hasMany(Contact::class);
}

And in the CrudController of Contact I've added:

class ContactCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\InlineCreateOperation;
...

At the end in the OrganizationCrudController I've add the field

CRUD::addField([
    'name' => 'contacts',
    'label' => 'Contacts',
    'type' => 'relationship',
    'ajax' => true,
    'inline_create' => [
        'entity' => 'contact'
    ],
    'attribute' => 'fullname_with_email',
    'wrapper' => ['class' => 'form-group col-md-12']
]);

??

What I expected to happen

to see the add item button ??

What happened

nothing appears. the select2 ajax remains to choose the contacts already present ??

What I've already tried to fix it

I tried to see how the relationship field is made and I saw that the "fetch_or_create" is not present in the HasMany relationship type. The fetch_or_create blade is not included on the switch where you check the relationship type in the hasMany case ??

Is it a bug in the latest version of Backpack?

yes After I run composer update backpack/crud the bug... is it still there? yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

8.2.19

PHP EXTENSIONS:

Core, bcmath, calendar, ctype, date, filter, hash, iconv, json, SPL, pcre, random, readline, Reflection, session, standard, mysqlnd, tokenizer, zlib, libxml, dom, PDO, bz2, SimpleXML, xml, xmlreader, xmlwriter, openssl, curl, ftp, fileinfo, gd, intl, imap, mbstring, exif, mysqli, Phar, pdo_mysql, sodium, xsl, zip, redis, imagick, mongodb

LARAVEL VERSION:

11.28.1.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.6 backpack/crud: 6.7.41 backpack/editable-columns: 3.0.10 backpack/generators: v4.0.7 backpack/pro: 2.2.21 backpack/theme-tabler: 1.2.14

karandatwani92 commented 4 weeks ago

Hey @pxpm

Here is the TLDR:

Do we have a reason for this behavior, or is it a BUG?

pxpm commented 4 weeks ago

Hey @karandatwani92 probably it's missing from the docs. InlineCreate can't work on HasMany relations, because to create an "HasMany" relation, you need the key for the entry you are creating, eg: Articles -> HasMany -> Categories.

To create a category linked to the article, there is an article_id in the categories table. If you are creating an article, you don't have an article_id yet to create a category.

We can probably make it work on "update", because there we already have the article id, but it's not in the plans for now. Maybe if we get the chance to work on an InlineUpdateOperation we maybe can have a look at that.

Can you please send a PR to docs mentioning that InlineCreate only works for belongsTo and ToMany relation types?

Cheers