barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.12k stars 1.15k forks source link

when i use the firstOrCreate(orm)is not find,can help me ,THS #1107

Open burtgithub opened 3 years ago

burtgithub commented 3 years ago

I created a test demo in Github

 https://github.com/burtgithub/laravel-ide-helper-test/

My environment

   php:with Zend OPcache v7.3.8
   laravel:Laravel Framework 7.30.0

My steps

   composer create-project laravel/laravel=7.* demoide --prefer-dist
   579  history 
   580  cd demoide/
   581  composer require --dev barryvdh/laravel-ide-helper
   582  pwd
   583  ./artisan ide-helper:generate
   584  php artisan cache:clear
   585  php artisan ide-helper:generate

   add the package to the extra.laravel.dont-discover key in composer.json, e.g.
   "extra": {
     "laravel": {
       "dont-discover": [
         "barryvdh/laravel-ide-helper"
       ]
     }
   }

   Add the following class to the providers array in config/app.php:
   Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

Create two test files

1:laravel-ide-helper-test/app/Http/Models/TestDemo.php

   <?php

   namespace App\Http\Models;

   use Illuminate\Database\Eloquent\Model;

   class TestDemo extends Model
   {
          protected $table = 'tbl_test';
          protected $connection = 'test';
          protected $guarded = [];

   }

2 /laravel-ide-helper-test/app/Http/Controllers/TestController.php

   <?php

   namespace App\Http\Controllers;
   use App\Http\Models\TestDemo;

   class TestController extends Controller
   {
          public function test(){

              $test   =   new TestDemo();

           $test->firstOrCreate();//in ide is not find

       }

   }

I don't know if I made a mistake or if I missed something

burtgithub commented 3 years ago

@mfn Please help me ,thank you

burtgithub commented 3 years ago

image

mfn commented 3 years ago

Yeah I guess that's not supported.

As you may or not may be aware, you can only "declare" a method with a name either static or not.

Right now the _ide_helper.php generates stubs for the static versions: image

Trying to have both results a) in an error in the IDE (not at runtime, because the file is not executed) and b) only one of them (the first entry) working anyway: image

The most common case is, AFAIK, to use Model::firstOrCreate() but yes, technically -> is valid.

So either use the :: version or, if you can't, you're currently out of luck regarding error in PhpStorm.

At least, I don't know a way to teach this to PhpStorm.