Crud: Potential Bug? addFields() that join other table will return incorrect field array

Created on 13 Mar 2020  Â·  4Comments  Â·  Source: Laravel-Backpack/CRUD

Bug report

What I did

Assume we have a setupupdateOperation() and setup() like this

    public function setup()
    {
        $this->crud->setModel(MyEvent::class);
        $this->crud->setEntityNameStrings(trans('backpack.application'), trans('backpack.applications'));
        $this->crud->setRoute(backpack_url('application'));
    }


    protected function setupUpdateOperation(){
        $this->crud->addFields([
            [
                'name'  => 'my_evt_registration.application_status',
                        'label' => 'Application Status',
                        'type'  => 'text',
            ],
                ]
        }

As you can see, there is a model/table called MyEvent and it joins (1-to-1) to another table called my_evt_registration.

What I expected to happen

I expected that it will correctly identify the database table and column it has. In this case database table is my_evt_registration and column is application_status.

What happened

However, as it turns out, when I am using dd() to debug, here is what I found

// Expected Result
array:3 [â–¼
  "name" => "application_status"
  "type" => "text"
  "label" => "Application Status"
]
// Incorrect Result
array:1 [â–¼
  "application_status" => array:3 [â–¼
    "name" => "vetting.application_status"
    "label" => "Application Status"
    "type" => "text"
  ]
]

Result in:

ErrorException
Undefined index: name

The file that is in question is this: \vendor\backpack\crud\src\app\Library\CrudPanel\Traits\Update.php.

Backpack, Laravel, PHP, DB version

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

PHP VERSION:

PHP 7.3.10 (cli) (built: Sep 24 2019 11:59:22) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.10, Copyright (c) 1998-2018 Zend Technologies

LARAVEL VERSION:

v6.16.0@b47217e41868d3049ec545cbb713aa94c6f39e55

BACKPACK VERSION:

4.0.53@2305b74390a58dc1b7e9b2c5798a663f60cdcd62

Bug

Most helpful comment

You can fix this temporarily by labeling the field yourself with the key:

$this->crud->addFields([
      'my_evt_registration.application_status' => [
          'name'  => 'my_evt_registration.application_status',
                'label' => 'Application Status',
                'type'  => 'text',
      ],
]);

By the way are you sure you are dumping a correct thing? I mean "name" => "vetting.application_status" is nowhere near 'name' => 'my_evt_registration.application_status',

OK, after reading your code, I did not use your method. Instead I do this:

$this->crud->addFields([
        [
            'name'  => 'application_status',    // This is a real db column name
                'label' => 'Application Status',
                'entity' => 'my_evt_registration',  // This is my 1-to-1 laravel model name, use the function name in Laravel, not table name.  
// IN this case "Event" model has a 1-to-1 relationship to another model called "My_Event_Registration", 
// the "Event" model has a function called "my_evt_registration" that 
// contains the "hasOne()" function, so we put the key "entity" here.  
// "entity" is backpack reserved keyword.
                'type'  => 'text',
        ],
]);

It works.

All 4 comments

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

You can fix this temporarily by labeling the field yourself with the key:

$this->crud->addFields([
        'my_evt_registration.application_status' => [
            'name'  => 'my_evt_registration.application_status',
                'label' => 'Application Status',
                'type'  => 'text',
        ],
]);

By the way are you sure you are dumping a correct thing? I mean "name" => "vetting.application_status" is nowhere near 'name' => 'my_evt_registration.application_status',

You can fix this temporarily by labeling the field yourself with the key:

$this->crud->addFields([
      'my_evt_registration.application_status' => [
          'name'  => 'my_evt_registration.application_status',
                'label' => 'Application Status',
                'type'  => 'text',
      ],
]);

By the way are you sure you are dumping a correct thing? I mean "name" => "vetting.application_status" is nowhere near 'name' => 'my_evt_registration.application_status',

My bad, I am trying to hide my custom field, but it seems I failed. :D

You can fix this temporarily by labeling the field yourself with the key:

$this->crud->addFields([
      'my_evt_registration.application_status' => [
          'name'  => 'my_evt_registration.application_status',
                'label' => 'Application Status',
                'type'  => 'text',
      ],
]);

By the way are you sure you are dumping a correct thing? I mean "name" => "vetting.application_status" is nowhere near 'name' => 'my_evt_registration.application_status',

OK, after reading your code, I did not use your method. Instead I do this:

$this->crud->addFields([
        [
            'name'  => 'application_status',    // This is a real db column name
                'label' => 'Application Status',
                'entity' => 'my_evt_registration',  // This is my 1-to-1 laravel model name, use the function name in Laravel, not table name.  
// IN this case "Event" model has a 1-to-1 relationship to another model called "My_Event_Registration", 
// the "Event" model has a function called "my_evt_registration" that 
// contains the "hasOne()" function, so we put the key "entity" here.  
// "entity" is backpack reserved keyword.
                'type'  => 'text',
        ],
]);

It works.

Was this page helpful?
0 / 5 - 0 ratings