【问题标题】:Laravel IDE autocomplete on models模型上的 Laravel IDE 自动完成
【发布时间】:2017-09-29 07:53:18
【问题描述】:

我有一个小问题,当您从数据库中检索对象时,是否可以检查该对象上的哪些属性?我已经用 Laravel ide helper (https://github.com/barryvdh/laravel-ide-helper) 生成了 phpDocs,但我没有看到任何属性..

这是我的代码:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Planning
 *
 * @mixin \Eloquent
 * @property int $id
 * @property string $name
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @method static \Illuminate\Database\Query\Builder|\App\Planning whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Planning whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Planning whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Planning whereUpdatedAt($value)
 */
class Planning extends Model
{


    function test()
    {
        $planningItems = App\Planning::all();

        foreach ($planningItems as $item) {
             echo $item->?;
        }
    }


}

【问题讨论】:

  • 您好 Vladyslav ,是的,我需要 $name ,我可以使用 $item->name 访问它,但没有办法自动完成它,以便 IDE 显示有哪些属性(想象一下15 个属性)?我正在使用 Netbeans。谢谢

标签: php laravel autocomplete


【解决方案1】:

实例化模型时是否会自动完成并立即使用它?

$planning = new App\Planning();
$planning->??

这对我有用。如果它也适用于您,但 OP 中的示例不起作用,则可能需要提示该方法,如下所示:

@method static \App\Planning[] all()

或者:

function test()
{
    /** @var App\Planning[] $planningItems */
    $planningItems = App\Planning::all();

    foreach ($planningItems as $item) {
         echo $item->?;
    }
}

如果这些都不起作用,那么可能是时候向 IDE 的制造商提出问题了。

【讨论】:

    【解决方案2】:

    我将用我认为最终可能解决这个问题的方法来重新提出这个问题。经过研究,它需要 PHP 8 并且很可能是最新的 eloquent 版本。但这个包似乎完全符合您的要求。

    https://github.com/lepikhinb/laravel-fluent

    使用 laravel-fluent,您可以像定义任何其他类一样定义模型属性。这些值将根据属性的本机类型转换为相应的类型。

    Before: 
    <?php
    /**
     * @property Collection $features
     * @property float $price
     * @property int $available
     */
    class Product extends Model
    {
        protected $casts = [
            'features' => 'collection',
            'price' => 'float',
            'available' => 'integer',
        ];
    }
    
    After:
    <?php
    
    class Product extends Model
    {
        use Fluent;
    
        public Collection $features;
        public float $price;
        public int $available;
    }
    

    如果您卡在旧版本或其他任何东西上,这对您没有帮助,因为这需要我们在现代 PHP 8 中拥有的所有那些不错的新玩具。但我认为对于这些天维护项目的人来说,这可能不是这样的问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 2011-06-21
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      相关资源
      最近更新 更多