Laravel nova“类名必须是有效对象或字符串”在自我BelongsTo字段上

问题描述 投票:0回答:1

这是我的“标准”资源代码

class Standard extends Resource
{

    public static $model = '\\PackageName\\Http\\Models\\Standard';

    public static $title = 'parent_id';

    public static $search = [
        'id',
    ];

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),

            Text::make('ASN Id', 'short_asn_id')
            ->sortable(),

            BelongsTo::make('Parent', 'getParent', 'app\Nova\Standard'),
            HasMany::make('Children', 'getchildren', 'app\Nova\Standard'),
        ];
    }
}

像这样的模型代码=>

public function getParent()
{
    return $this->belongsTo(static::class, 'parent_id');
}

public function getChildren()
{
    return $this->hasMany(static::class, 'parent_id','id');
}

我想在同一个表上建立父子关系。不幸的是我经常得到“类名必须是有效的对象或字符串”错误。

我尝试传递类名称\ App \ Nova \ Standard :: class不工作。

我是Nova的新手,无法弄清楚确切的问题。

Nova版本是最新版本。 Laravel版本是5.7

php laravel laravel-nova
1个回答
0
投票

仔细检查您的模型和资源命名空间。

为什么你的\\属性中有$model?通常命名空间为App\Standard,如果你有不同的命名空间相应地修改。通常,命名空间中的应用程序是capital,App\Nova\Standard

class Standard extends Resource
{

    public static $model = 'App\Standard'; // Modify according to your namespace
    ...

    BelongsTo::make('Parent', 'getParent', 'App\Nova\Standard'), // Capital A
}
© www.soinside.com 2019 - 2024. All rights reserved.