Giter Club home page Giter Club logo

Comments (14)

ellisio avatar ellisio commented on May 13, 2024

A fix for this was to do the following for my model:

Change:

class Model extends \Eloquent {

To:

use \Illuminate\Database\Eloquent\Model as Eloquent;
class Model extends Eloquent {

Kind of stinks to do that, but it works and removes the errors in PhpStorm...

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

Hmm yes I understand.
The problem is that Laravel uses facades to use the static syntax on non-static functions, but it isn't always consistent. I guess it should work when I just extend the \Illuminate\Database\Eloquent\Model instead of the QueryBuilder, but then some other functions will be missing, so I don't really see an optimal way to handle this.

So I'm open to ideas ;)

from laravel-ide-helper.

ellisio avatar ellisio commented on May 13, 2024

I think having something in the module's README pointing out this "issue" with my fix with use the use syntax on the model would be the best way.

Kind of a hassle, but I guess that's the side effect of using static methods in the core. haha

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

Using \Illuminate\Database\Eloquent\Model instead of Eloquent has the downside that you lose the autocomplete on the models. I added an option in the config to skip certain classes, that are in the facade root, but shouldn't be static.
freshTimestamp is added by default, but if there are other classes that don't belong there, please let me know!

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

Ah, now that I think of it, instead of skipping, it is probably better to make them non-static. But should still fix your issue ;)

from laravel-ide-helper.

ellisio avatar ellisio commented on May 13, 2024

So this is interesting. Here is an example _ide_helper_models.php of mine: http://paste.laravel.com/r23

I have this in one of my controllers:

/** @var \v1\Metadata\Artist $artist */
$artist = \v1\Metadata\Artist::find(1);
if ($artist->source == 1) return;

Using PhpStorm, the "source" auto complete does not work. The message I get when hovering that pointer is:

Field accessed via magic method

I'm going to tweak the _ide_helper_models.php class manually as I have a hunch as to why this is happening, but I'm not 100% certain.

from laravel-ide-helper.

ellisio avatar ellisio commented on May 13, 2024

As I thought, if you format the _ide_helper_models.php like this methods AND properties work correctly with PhpStorm:

<?php

namespace v1\Metadata;
/**
 *
 * Generated properties for v1\Metadata\Artist
 *
 * @property integer $id
 * @property string $source_id
 * @property string $source
 * @property string $name
 * @property string $view_url
 * @property integer $created_at
 * @property integer $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|v1\Metadata\Artist\Attribute[] $attributes
 * @property-read \Illuminate\Database\Eloquent\Collection|v1\Metadata\Album[] $albums
 * @property-read \Illuminate\Database\Eloquent\Collection|v1\Metadata\Song[] $songs
 * @property-read v1\Metadata\Artist\Biography $biography
 *
 */
class Artist {}

I got this idea from looking at the bootstrap\compiled.php file as that's how Taylor formats that file.

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

Hmm, I didn't use namespaces with the models indeed.

But are you using the ide_helpers_models.php file as it is generated? I would suggest you copy the generated properties and place them in the real Model, and then just delete the file.
If you have both that file and the model declarations, phpStorm doesn't know which one to choose.

And do you need that inline @var doc? With the latest changes, find() should return static, which phpStorm would interpret as the calling class.

from laravel-ide-helper.

ellisio avatar ellisio commented on May 13, 2024

I'm moving docs over now. I've had issues with find()'s @return doc and PhpStorm in the past; but let's see if the latest EAP works this way. I'll report back shortly.

from laravel-ide-helper.

ellisio avatar ellisio commented on May 13, 2024

Feature request though, is there a way you can add in a way to generate PHPDoc for a specific model? That way I don't have to run the command then hunt down the model I need? As our app grows we'll have around 70-90 models...

And one more thing, on the generated @property-read you need to prefix the classes with a backslash. For example:

@property-read v1\Metadata\Album $album

Needs to be:

@property-read \v1\Metadata\Album $album

As when in the Artist model, we are in the v1\Metadata namespace so your current methodology is looking for v1\Metadata\v1\Metadata\Album.

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

You can do php artisan ide-helper:models MyModel but I'm not sure if that works with namespaces.

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

Should work now, but you do have to use \ instead of
php artisan ide-helper:models v1\\Metadata\\Album

And the helper files should have correct namespaces also.

from laravel-ide-helper.

ellisio avatar ellisio commented on May 13, 2024

Awesome, namespace model generation now works. Thanks!

Looks like that we have the following outstanding:

  1. Change @property-read v1\Metadata\Album $album to be @property-read \v1\Metadata\Album $album

Additional notes:

  1. ::find() returns \Illuminate\Database\Eloquent\Model so the @var declaration is indeed needed :(

Thanks a lot for the quick response and support! This utility has saved me a lot of time generating PHPDoc for my autocompletion needs. It's almost perfect! :)

from laravel-ide-helper.

barryvdh avatar barryvdh commented on May 13, 2024

Okay, that first one should be fixed now I think.

The second, that is weird. Do you still use use \Illuminate\Database\Eloquent\Model as Eloquent; ?
If so, remove that line, it shouldn't be needed. And if you use it, you don't use the find() method from my IDE helper.
If that is not the issue, what does the the find() in Eloquent in _ide_helper.php return? Mine has @return static.

from laravel-ide-helper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.