2

I have this function that receives a "user" model by parameter , I want to collect the properties of that object, for this I do it this way, the code works for me but the editor "phpstorm" complained to me with this error and it was to know what would be the best way to do this.

Thank you

public function sendEmail(User $user)
{
    $params = [
        'name' => "{$user->completeName}",
        'language' => "{$user->locale}",
        'user_id' => "{$user->id}"
    ];

}

Field accessed via magic method less... (Ctrl+F1) Inspection info: Referenced field is not found in subject class. Note: Check is not performed on objects of type "stdClass" or derived.

Thanxs,

edica
  • 339
  • 3
  • 9
  • 16
  • It's just a warning, you can disable it. Similar to this question: https://stackoverflow.com/questions/25578649/phpstorm-field-accessed-via-magic-method – thchp Nov 16 '18 at 08:59

2 Answers2

2

maybe this is simpler

$params = $user->toArray();

or

$params = $user->toJson();
simonecosci
  • 1,194
  • 7
  • 13
1

That's because in Laravel your model does not actually have the properties defined. In PHP there is the concept of magic methods though ( http://php.net/manual/en/language.oop5.overloading.php#object.get ), where the __get() method allows you to basically intercept the access to an otherwise inaccessible (or non-existing) property.

This is what happens behind the scenes in Laravel. All your property accesses are "intercepted" and Laravel looks if your database contains a column which is named like the property you are trying to access (very simplified speaking).

In a Laravel context you can savely ignore this warning.

Fitzi
  • 1,641
  • 11
  • 17