php 如何在Laravel中为模型实现代码完成

ryoqjall  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(116)

我正在使用VSCode学习Laravel。当我编写“Model::“或“Model::query()->”方法时,我遇到了一个问题,比如“count”和“insert”不在代码完成中。我安装的相关VSCode扩展:

  • 因特莱芬斯
  • amir9480/vscode-laravel-extra-intellisense
// $result = Category::query()->insert($categories);
$result = Category::insert($categories);
$this->assertTrue($result);

// $total = Category::query()->count();
$total = Category::count();
$this->assertEquals(10, $total);

我已经安装了“barryvdh/laravel-ide-helper”,但它只生成“whereColumn()”方法。

/**
 * App\Models\Category
 *
 * @property int $id
 * @property string $name
 * @property string $description
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Category newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Category query()
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Category whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Category extends Model 
{
 ...
}

看起来我可以在模型上使用“Illuminate\Database\Query\Builder”中的方法。所以我想在写“Model::“或者“Model::query()->”的时候我得到了类的代码完成方法

bq3bfh9z

bq3bfh9z1#

不存在\Eloquent类。您希望包含在@mixin指令中的是Illuminate\Database\Eloquent\Builder
我使用PhpStorm,但效果应该是相同的,在任何一个程序。我的类头和文档块看起来像这样:

<?php

namespace App\Models;

use Database\Factories\App\CategoryFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;

/**
 * @mixin Builder
 * @property int $id
 * @property string $name
 * @property string $description
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read User $user
 * @property-read Collection<Subcategory> $subcategories
 * @method static Builder onlyTrashed()
 * @method static Builder withTrashed()
 * @method static Builder withoutTrashed()
 * @method static Builder newModelQuery()
 * @method static Builder newQuery()
 * @method static Builder query()
 * @method static Builder whereCreatedAt($value)
 * @method static Builder whereDescription($value)
 * @method static Builder whereId($value)
 * @method static Builder whereName($value)
 * @method static Builder whereUpdatedAt($value)
 * @method static CategoryFactory factory(...$parameters)
 */

class Category extends Model
{
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function subcategories(): HasMany
    {
        return $this->hasMany(Subcategory::class);
    }
}

您还应该为每个关系方法包含一个@property-read指令(我在这里包含了几个示例),如果您正在使用工厂,还应该为工厂包含一个@method指令。

相关问题