php PhpStorm 中的 Eloquent ORM 代码提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29439753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent ORM Code Hinting in PhpStorm
提问by Josh Janusch
So I'm just starting off with Laravel (using v5) and Eloquent. I'm working on getting some basic APIs up and running and noticing that a lot of working methods don't show up in PhpStorm's code hinting
所以我刚开始使用 Laravel(使用 v5)和 Eloquent。我正在努力启动和运行一些基本的 API,并注意到很多工作方法没有出现在 PhpStorm 的代码提示中
So I have this model:
所以我有这个模型:
namespace Project\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model
implements AuthenticatableContract, CanResetPasswordContract {
}
And in one of my controllers I try to do
在我的一个控制器中,我尝试做
User::query()->orderBy('id', 'desc');
User::query()
creates a Eloquent Builder
object and orderBy()
behave properly and without error. However, PhpStorm does not show orderBy()
(or take()
, skip()
, and I'm sure others) when I type User::query()->
and gives warnings when I actually do use it.
User::query()
创建一个 EloquentBuilder
对象并且orderBy()
行为正确且没有错误。但是,PhpStorm在我键入时不显示orderBy()
(或take()
, skip()
,我敢肯定其他人)User::query()->
并在我实际使用它时发出警告。
I am using Laravel IDE Helperwhich has helped immensely with bringing code hints to the Facades, but not to the models/builders it would seem.
我正在使用Laravel IDE Helper,它极大地帮助了为 Facades 带来代码提示,但对模型/构建器似乎没有帮助。
Does anyone have a solution to this?
有没有人有解决方案?
回答by Erik Johansson
For future Googlers, and perhaps OP as well if you are still sticking to Laravel.
对于未来的 Google 员工,如果您仍然坚持使用 Laravel,或许也可以使用 OP。
The laravel-ide-helperpackage solves this issue for you quite elegantly, with what I believe is a relatively new feature; generated model PHPDocs.
该laravel-IDE辅助包解决了这个问题对你相当漂亮,与我所相信的是一个相对较新的功能; 生成模型 PHPDocs。
You can generate a separate file for all PHPDocs with this command:
您可以使用以下命令为所有 PHPDoc 生成一个单独的文件:
php artisan ide-helper:models
The generated metadata will look something like this for each class:
对于每个类,生成的元数据将如下所示:
namespace App {
/**
* App\Post
*
* @property integer $id
* @property integer $author_id
* @property string $title
* @property string $text
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property-read \User $author
* @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
*/
class Post {}
}
This caused issues for me in PHPStorm however, where the software was complaining about multiple class definitions. Luckily an option is readily available for writing directly to the model files:
然而,这在 PHPStorm 中给我带来了问题,软件抱怨多个类定义。幸运的是,有一个选项可以直接写入模型文件:
php artisan ide-helper:models -W
There are a few more options and settings available if you need to tweak the behavior, but this is the gist of it.
如果您需要调整行为,还有更多选项和设置可用,但这是它的要点。
回答by Денис Чорний
Add in model PHPDoc@mixin
添加模型 PHPDoc@mixin
/**
* Class News
* @property int $id
* @property string $created_at
* @property string $updated_at
* @mixin \Eloquent
* @package App
*/
class News extends Model
{
}
In PHPStormworks
在PHPStorm 中工作
Or add to \Illuminate\Database\Eloquent\Model
或添加到 \Illuminate\Database\Eloquent\Model
PHPDoc
PHP文档
/**
* @mixin \Eloquent
*/
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
...
回答by Evren Yurtesen
A little late but I recently had the same problem so I thought I would put a note down:
有点晚了,但我最近遇到了同样的问题,所以我想我会记下:
This is because Database\Eloquent\Model.php
has a query()
function which returns \Illuminate\Database\Eloquent\Builder
and the Eloquent\Builder
has a line:
这是因为Database\Eloquent\Model.php
有一个query()
返回的函数\Illuminate\Database\Eloquent\Builder
并且Eloquent\Builder
有一行:
use Illuminate\Database\Query\Builder as QueryBuilder;
Then it uses 'magic' __call methods to call to functions in Query\Builder
. (look for __call
method in Eloquent\Builder
)
然后它使用“魔法” __call 方法来调用Query\Builder
. (在 中寻找__call
方法Eloquent\Builder
)
See: http://php.net/manual/en/language.oop5.overloading.php#object.call
请参阅:http: //php.net/manual/en/language.oop5.overloading.php#object.call
__call() is triggered when invoking inaccessible methods in an object context.
__call() 在对象上下文中调用不可访问的方法时触发。
So, indeed the method you are calling is inaccessible :) There is not much that the IDE can do.
因此,确实无法访问您正在调用的方法:) IDE 可以做的并不多。
There are workarounds like using @method tags but it is unmaintainable. An alternative is to use @mixin (but this is not standards based). See: https://github.com/laravel/framework/issues/7558
有一些变通方法,例如使用 @method 标签,但它无法维护。另一种方法是使用@mixin(但这不是基于标准的)。参见:https: //github.com/laravel/framework/issues/7558
I think this all be resolved when they get rid of all the magic calls in the Laravel code and use PHP 'traits' instead. See last message here.:)
我认为当他们摆脱 Laravel 代码中的所有魔法调用并使用 PHP 'traits' 时,这一切都会得到解决。请参阅此处的最后一条消息。:)
回答by mirza
You can try Laravel plug-infor PhpStorm and you need to specifically activate it in your project settings.
你可以试试PhpStorm的Laravel插件,你需要在你的项目设置中专门激活它。
回答by GTCrais
If you're using BarryVHD's Laravel IDE Helperpackage, run:
如果您使用的是 BarryVHD 的Laravel IDE 帮助程序包,请运行:
php artisan ide-helper:eloquent
This will write /** @mixin \Eloquent */
into the vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php
file.
这将写入/** @mixin \Eloquent */
到vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php
文件中。
回答by Isometriq
I wanted to have some kind of explicit "casting" when interacting with the query builder. Example...
我想在与查询构建器交互时进行某种显式的“转换”。例子...
$user = User::query()->findOrFail($id);
$user->myUserSpecialMethod(); // <-- IDE syntax error
Since all my models are extending my custom base Model which in turn extends Eloquent, I've ended up creating this method in my custom base model:
由于我的所有模型都扩展了我的自定义基础模型,而后者又扩展了 Eloquent,因此我最终在自定义基础模型中创建了此方法:
/**
* Explicit type-hinting
*
* @return static
*/
static public function hint(BaseModel $model)
{
return $model;
}
This way, it solves the IDE invalid error and helps me:
这样,它解决了 IDE 无效错误并帮助我:
$user = User::hint(User::query()->findOrFail($id));
$user->myUserSpecialMethod(); // <-- all OK !
Please note that this is not OOP type casting. It is only a hint to help the IDE. In my example, the returned Model
was already a User
. If I woud use this method on a derived class like SuperUser
, only the IDE will be fooled...
请注意,这不是 OOP 类型转换。这只是帮助 IDE 的提示。在我的例子中,返回的Model
已经是一个User
. 如果我在像这样的派生类上使用这个方法SuperUser
,只有 IDE 会被愚弄......
An nice alternative also is to put meta information directly over the assignment statement:
一个不错的选择也是将元信息直接放在赋值语句上:
/** @var User $user */
$user = User::query()->findOrFail($id);
$user->myUserSpecialMethod(); // <-- all OK !
Or next to it...
或者旁边...
$user = User::query()->findOrFail($id); /** @var User $user */
$user->myUserSpecialMethod(); // <-- all OK !