[Bug]: Eloquent model attributes shown as unused
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
- 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([]);
}
},
);
}
}
- Use the custom attribute somewhere in your code:
$store = Store::create();
$hours = $store->openingHours;
dd($hours);
- Observe that Laravel Idea still does not detect any usages of
openingHours().
Relevant log output
n/a
Hello.
Have you tried this setting? https://github.com/laravel-idea/plugin/issues/1206#issuecomment-2989827966
Hello.
Have you tried this setting? #1206 (comment)
Hi, the setting is enabled. Unfortunately it does not resolve the issue
Does it find usages by Cmd-Click?
Does it find usages by Cmd-Click?
It does not find any:
However, there are quite some:
Another example with fullAddress:
Usages:
What type does the tenant() function return? Is it this model?
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.
We have the same issue, converted attributes for method that were previously just returning bool, so we can use caching, like so:
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.
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...
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.