I use PhpStorm & have been noticing when I try to review the automated warning & error that it shows me, on every piece of code that I am learning it gives a warning:
Property accessed via the "Magic Method"
As it is a warning, I know it not anything critical to the functionality of the code.
I am just interested to know why it is giving this as a warning & if there is any work around or if I should just ignore this and forget about what the magic method is even referring too.
A lot of properties are accessed via magic methods and it's by design. You can generate helper files and PHPDoc blocks to get rid of these messages. You get the added benefits of auto-completion and better error checking. A commonly used library is barryvdh/laravel-ide-helper. I also use the Laravel Idea plugin, which has a small subscription fee.
I would suggest getting the Laravel Idea plugin for PHPStorm and then using its "Generate Helper Code" function. This will fix those messages for you (just remember to regenerate after you make any changes to your models).
@JohnnyW2001 Note that you'll also need to add /** @mixin Model */ before your resource class for the Laravel Idea plugin to stop showing squiggly lines below magically accessed properties.
For example:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin User */
class UserResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
// ...
];
}
}