PhpStorm 中的 Eloquent ORM 代码提示 [英] Eloquent ORM Code Hinting in PhpStorm

查看:75
本文介绍了PhpStorm 中的 Eloquent ORM 代码提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚开始使用 Laravel(使用 v5)和 Eloquent.我正在努力启动和运行一些基本的 API,并注意到很多工作方法没有出现在 PhpStorm 的代码提示中

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

所以我有这个模型:

namespace ProjectModels;

use IlluminateDatabaseEloquentModel;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword 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() 创建一个 Eloquent Builder 对象并且 orderBy() 行为正常且没有错误.但是,当我输入时,PhpStorm 不显示 orderBy()(或 take()skip(),我确定其他人)User::query()-> 并在我实际使用时发出警告.

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.

我正在使用 Laravel IDE Helper,它极大地帮助了 Facades 的代码提示,但不是看起来像是模型/建造者.

I am using Laravel IDE Helper which has helped immensely with bringing code hints to the Facades, but not to the models/builders it would seem.

有没有人有办法解决这个问题?

Does anyone have a solution to this?

推荐答案

对于未来的 Google 员工,如果您仍然坚持使用 Laravel,也许也适用于 OP.

For future Googlers, and perhaps OP as well if you are still sticking to Laravel.

laravel-ide-helper 包非常优雅地为您解决了这个问题,我认为这是一个相对较新的功能;生成模型 PHPDocs.

The laravel-ide-helper package solves this issue for you quite elegantly, with what I believe is a relatively new feature; generated model PHPDocs.

您可以使用以下命令为所有 PHPDoc 生成一个单独的文件:

You can generate a separate file for all PHPDocs with this command:

php artisan ide-helper:models

为每个类生成的元数据如下所示:

The generated metadata will look something like this for each class:

namespace App {
/**
 * AppPost
 *
 * @property integer $id
 * @property integer $author_id
 * @property string $title
 * @property string $text
 * @property CarbonCarbon $created_at
 * @property CarbonCarbon $updated_at
 * @property-read User $author
 * @property-read IlluminateDatabaseEloquentCollection|Comment[] $comments
 */
class Post {}
}

然而,这在 PHPStorm 中给我带来了问题,其中软件抱怨多个类定义.幸运的是,有一个选项可以直接写入模型文件:

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:

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.

这篇关于PhpStorm 中的 Eloquent ORM 代码提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆