13

Laravel 5.5 has a new API Resources feature, and it nicely redirects calls to model attributes (like $this->id). I use ide-helper:models to generate phpdocs for models that type-hints all model attributes. However, this does not apply to a resource and I get "Field accessed via magic method" squigglies. Is there a way to point it to model's phpdoc without copying it?

3
  • Why would you ever have to get the properties of a resource in your PHP code? I think you are misunderstanding what API resources are meant for. An API ressource is meant to convert a model into a easy object to return to the HTTP response. Commented Oct 2, 2017 at 14:07
  • How am I supposed to convert properties without getting them? My toArray method looks like return ['id' => $this->id, 'name' => $this->name]; and I have no autocompletion on $this properties Commented Oct 2, 2017 at 14:41
  • Ooh sorry. I misunderstood your question then. I added an answer. Commented Oct 2, 2017 at 20:48

1 Answer 1

47

You can use the @mixin

Here is an example, If you want the properties/phpdocs from the User model in your User Resource, then do like this

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

/**
 * Class User
 *
 * @mixin \User
 * */
class User extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh man! Just came across this today and wow! What a savior!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.