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] Advanced use case issue for column type text #5645

Closed
mialex opened this issue Sep 1, 2024 · 4 comments
Closed

[Bug] Advanced use case issue for column type text #5645

mialex opened this issue Sep 1, 2024 · 4 comments
Assignees

Comments

@mialex
Copy link

mialex commented Sep 1, 2024

Bug report

For columns type text we can use 1-to-1 relation.

[
    'name'  => 'parent.title',
    'label' => 'Title',
    'type'  => 'text'
],

In my case I have a user model with the email field. For the email field, I have a mutator

    protected function email(): Attribute
    {
        return Attribute::make(
            set: fn ($value) => mb_convert_case($value, MB_CASE_LOWER, 'UTF-8'),
        );
    }

What I expected to happen

I would expect to get my email value when I show the column

What happened

Instead, I get an error Call to undefined method ...Models\User::email()

What I've already tried to fix it

The issue is here

public function getOnlyRelationEntity($field)
    {
        $entity = isset($field['baseEntity']) ? $field['baseEntity'].'.'.$field['entity'] : $field['entity'];
        $model = $this->getRelationModel($entity, -1);
        $lastSegmentAfterDot = Str::of($field['entity'])->afterLast('.');

        if (! method_exists($model, $lastSegmentAfterDot)) {
            return (string) Str::of($field['entity'])->beforeLast('.');
        }

        return $field['entity'];
    }

Particularly in tuis, please
if (! method_exists($model, $lastSegmentAfterDot)) {

I would say that you need to use reflection to define is this is the Attribute method or no.
See: https://laravel.com/docs/11.x/eloquent-mutators#defining-a-mutator

Backpack, Laravel, PHP, DB version

PHP VERSION:

8.3.10

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, sqlite3, zlib, ctype, curl, dom, fileinfo, filter, hash, iconv, json, mbstring, SPL, session, PDO, pdo_sqlite, bz2, posix, random, readline, Reflection, standard, SimpleXML, tokenizer, xml, xmlreader, xmlwriter, mysqlnd, amqp, apcu, bcmath, Phar, calendar, exif, gd, gettext, intl, mysqli, pcntl, pdo_mysql, redis, shmop, soap, sockets, sodium, sysvmsg, sysvsem, sysvshm, xsl, zip, Zend OPcache, xdebug

LARAVEL VERSION:

11.21.0.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.6
backpack/crud: 6.7.33
backpack/generators: v4.0.5
backpack/menucrud: v4.0.1
backpack/pagemanager: 3.3.0
backpack/permissionmanager: 7.2.1
backpack/settings: 3.1.1
backpack/theme-coreuiv4: 1.1.2

@pxpm
Copy link
Contributor

pxpm commented Sep 2, 2024

Hello @mialex

I don't get where parent.title relates to email 🤷 Care to provide some more details ?

Thanks

@karandatwani92
Copy link
Contributor

Hey @pxpm

He came up withparent.title as the same example we gave in our docs.

Anyway, I was able to reproduce the BUG. Here's how it goes:

  • Add a column CRUD::addColumn(['name' => 'owner.name']); in our Demo's InvoiceCRUD.
  • Above works fine until I add the following accessor to the Owner model.
protected function name(): Attribute
 {
     return Attribute::make(
         get: fn(string $value) => ucfirst($value),
         set: fn(string $value) => strtolower($value),
     );
}

@pxpm
Copy link
Contributor

pxpm commented Sep 2, 2024

Thanks @mialex for the report.

Exceptional work @karandatwani92 it helped a lot to just know where too look at 👍

This had been fixed in the latest backpack/crud 6.7.34 release.

Cheers

@mialex
Copy link
Author

mialex commented Sep 2, 2024

Yes, sorry, I copied wrong original column

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

3 participants