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] Cant store relationship (select 1-n) in 5.0.2 free version #4171

Closed
busaku opened this issue Feb 9, 2022 · 3 comments
Closed

[Bug] Cant store relationship (select 1-n) in 5.0.2 free version #4171

busaku opened this issue Feb 9, 2022 · 3 comments
Assignees
Labels
Possible Bug A bug that was reported but not confirmed yet. triage

Comments

@busaku
Copy link

busaku commented Feb 9, 2022

Bug report

What I did

Add "select" field to create and update operation()

What I expected to happen

I can save the data

What happened

I cant save the data

What I've already tried to fix it

Testing all addField([ // settings])

Is it a bug in the latest version of Backpack?

yes 5.0.2

Backpack, Laravel, PHP, DB version

PHP VERSION:

PHP 8.1.2 (cli) (built: Jan 26 2022 16:40:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies

LARAVEL VERSION:

v9.0.1@afeb58d019b203ecb1a1c4063a66a029ea6bea63

BACKPACK VERSION:

5.0.2@a118a414770e6719732c7629f939d4f9337f5433

Soooo,
My relationship in Rescuer Model:
return $this->hasOne(RescuerType::class, 'id', 'rescuer_type_id');
(Worked in laravel only)

Settings to the field:

$this->crud->addField([
            'label' => 'Typ',
            'type' => 'select',
            'name' => 'rescuer_type_id',
            'entity' => 'rescuerType'
        ]);

What i got i try to save a new or an updated rescuer:
Call to undefined method App\Models\Rescuer::rescuer_type_id()

So, i ment out, that he try to use the "name" as the relation-ship function in Rescuer.
But the docs says:

[  // Select
   'label'     => "Category",
   'type'      => 'select',
   'name'      => 'category_id', // the db column for the foreign key

   // optional
   // 'entity' should point to the method that defines the relationship in your Model
   // defining entity will make Backpack guess 'model' and 'attribute'
   'entity'    => 'category',

   // optional - manually specify the related model and attribute
   'model'     => "App\Models\Category", // related model
   'attribute' => 'name', // foreign key attribute that is shown to user

   // optional - force the related options to be a custom query, instead of all();
   'options'   => (function ($query) {
        return $query->orderBy('name', 'ASC')->where('depth', 1)->get();
    }), //  you can use this to filter the results show in the select
]

So, name should be the foreignkey. And entoty should be the relation-ship model.

if i just use this settings:

$this->crud->addField([
            'label' => 'Typ',
            'type' => 'select',
            'name' => 'rescuerType',
        ]);

(Caus u think well, than i just use the relationship function.) i got:
array_key_exists(): Argument #2 ($array) must be of type array, string given
File: vendor/backpack/crud/src/app/Library/CrudPanel/Traits/FakeFields.php:59

In Backpack version 4.1 i works well.. anybody got an idea?

Edit: I forgot, the form works well. i got an selection-box with the correct relation-ship values from the RescuerType table. Only on store the data, i got the error

@busaku busaku added the triage label Feb 9, 2022
@welcome
Copy link

welcome bot commented Feb 9, 2022

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@busaku busaku changed the title [Bug] [Bug] Cant store relationship (select 1-n) in 5.0.2 free version Feb 9, 2022
@tabacitu tabacitu added the Possible Bug A bug that was reported but not confirmed yet. label Feb 10, 2022
@pxpm
Copy link
Contributor

pxpm commented Feb 10, 2022

Hello @busaku

I don't think this is making sense to me. The relation you show is hasOne (1-1) and like the field says, it should be used in a BelongsTo (1-n).

So if you have rescuer_type_id in your Rescuer model, you sould be using:

public function rescuerType() {
    return $this->belongsTo(RescuerType::class, 'rescuer_type_id');
}

Let me know if that relation was the problem.

Pedro

@busaku
Copy link
Author

busaku commented Feb 10, 2022

Hey @pxpm

so.. it works. but now it doesnt make sense to me :D
but thank you ;)

paD

@busaku busaku closed this as completed Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Bug A bug that was reported but not confirmed yet. triage
Projects
None yet
Development

No branches or pull requests

3 participants