plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: Eloquent model attributes shown as unused

Open musa11971 opened this issue 2 months ago • 7 comments

Bug description

When defining a custom Eloquent model attribute, Laravel Idea does not detect its usage. The function name will highlight in gray and it will say "no usages". The IDE will suggest to remove the function, however in reality it might be in use by many other files.

Similar to #1206, but with attributes instead of scopes

Plugin version

11.4.1.252

Operating system

MacOS

Steps to reproduce

  1. Create a Laravel model with custom attribute:
class Store extends Model
{
    protected function openingHours(): Attribute
    {
        return Attribute::make(
            get: function () {
                try {
                    return OpeningHours::create($this->opening_hours_data ?? []);
                } catch (Exception) {
                    return OpeningHours::create([]);
                }
            },
        );
    }
}
  1. Use the custom attribute somewhere in your code:
$store = Store::create();
$hours = $store->openingHours;

dd($hours);
  1. Observe that Laravel Idea still does not detect any usages of openingHours().
Image

Relevant log output

n/a

musa11971 avatar Oct 17 '25 13:10 musa11971

Hello.

Have you tried this setting? https://github.com/laravel-idea/plugin/issues/1206#issuecomment-2989827966

adelf avatar Oct 17 '25 13:10 adelf

Hello.

Have you tried this setting? #1206 (comment)

Hi, the setting is enabled. Unfortunately it does not resolve the issue

musa11971 avatar Oct 17 '25 13:10 musa11971

Does it find usages by Cmd-Click?

adelf avatar Oct 17 '25 14:10 adelf

Does it find usages by Cmd-Click?

It does not find any:

Image

However, there are quite some:

Image

Another example with fullAddress:

Image

Usages:

Image

musa11971 avatar Oct 17 '25 14:10 musa11971

What type does the tenant() function return? Is it this model?

adelf avatar Oct 17 '25 14:10 adelf

What type does the tenant() function return? Is it this model?

It's the function provided by this package https://github.com/archtechx/tenancy and returns a normal Eloquent model. In my case App\Models\Tenant.

Even if I do something like:

dd(\App\Models\Tenant::first()->openingHours);

It will still mark it as unused in the Tenant model.

musa11971 avatar Oct 17 '25 21:10 musa11971

We have the same issue, converted attributes for method that were previously just returning bool, so we can use caching, like so:

Image

Re-generated helper code, and it's showing greyed out and no usages

Then, as you can see when I click the method, it finds the usages.

Image

At least here it shows the usages if we shift click the method name, however when moving Attributes (or Scopes) to a trait for deduplication, we get nothing...

Image

I would imagine many of your users utilise Model/Concerns/ to store trait that hold duplicate logic across Models, it would be really nice if we could have support for this.

Edit: I know the screenshot shows public method, but it's the same when corrected to protected too.

CamKemBell avatar Oct 19 '25 11:10 CamKemBell