ray icon indicating copy to clipboard operation
ray copied to clipboard

Appended Accessor with Attribute results in BadMethodCallException

Open TiiFuchs opened this issue 3 years ago • 2 comments

Ray Version 2.0.1 Laravel Version 9.9.0

I have a Eloquent model that uses the new Attribute syntax of Laravel to create an Accessor See here.

When I add an $appends property to my Eloquent Model so the newly created accessor also gets added to the json representation, ray crashes with a BadMethodCallException.

It tries to call get<propertyName>Attribute(), which does not exist, since I am using the version with Illuminate\Database\Eloquent\Casts\Attribute.

I think this might be a bug.

TiiFuchs avatar Apr 20 '22 17:04 TiiFuchs

Could you share a small example app that demonstrates the problem? That will help us getting it fixed faster.

freekmurze avatar Apr 21 '22 07:04 freekmurze

So I took another look this morning and I think I understand it now. It's more a laravel-ray issue and actually it's more like a user error than an actually issue. But I guess it could be fixed anyway. I'll let that for you to decide.

I created a test app and put it here: https://github.com/TiiFuchs/ray-test Running php artisan test throws this exception (no db needed for this): https://flareapp.io/share/17xj2kbm#F32

The problem is, that I used the camelCase version of the new Attribute like this: https://github.com/TiiFuchs/ray-test/blob/59ea5b550b6976375c24adddfe78b0a824fa9e81/app/Models/User.php#L49-L58

Ray expects this to be

  protected $appends = [
      'contact_line'
  ];

I didn't understand this at first, because Laravel seems to get along with the camelCase version just fine, but Ray didn't.

So the actual fix for the user is to just specify the attributes in $appends as snake_case. But if you think it's worth it, ray can become a little bit more robust.

TiiFuchs avatar Apr 21 '22 07:04 TiiFuchs

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

spatie-bot avatar Aug 22 '22 10:08 spatie-bot