Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] inlineCreate not working on hasMany relationship #5703

Open
mrinaldidfs opened this issue Oct 22, 2024 · 2 comments
Open

[Bug] inlineCreate not working on hasMany relationship #5703

mrinaldidfs opened this issue Oct 22, 2024 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@mrinaldidfs
Copy link

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
Copy link
Contributor

Hey @pxpm

Here is the TLDR:

  • While using inlineCreate withbelongsToMany - everything works.
  • While using inlineCreate with hasMany - the Add button is not included.
    Screenshot 2024-10-24 at 7 57 07 PM
    Screenshot 2024-10-24 at 7 59 19 PM

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

@pxpm
Copy link
Contributor

pxpm commented Oct 25, 2024

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

@pxpm pxpm assigned karandatwani92 and unassigned pxpm Oct 25, 2024
@pxpm pxpm added documentation Improvements or additions to documentation and removed Bug Priority: MUST Possible Bug A bug that was reported but not confirmed yet. labels Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Status: No status
Development

No branches or pull requests

3 participants