admin管理员组

文章数量:1122832

In Laravel 10 / nova 4.27 app ProductAttribute model belongs to Product model

class ProductAttribute extends Model
{
    protected $table = 'product_attributes';
    protected $primaryKey = 'id';
    protected $fillable = ['product_id', 'key', 'value' ];

    public function scopeGetByProductId($query, int $productId= null)
    {
        if (!empty($productId)) {
            $query->where($this->table . '.product_id', $productId);
        }
        return $query;
    }
    public function product(){
        return $this->belongsTo(Product::class, 'product_id','id');
    }

and Product model :

class Product extends Model implements HasMedia
{
    use Sluggable, InteractsWithMedia;
    use HasHeartbeats;

    protected $table = 'products';
    protected $primaryKey = 'id';

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

I created a ProductAttribute Repeatable component with command :

php artisan nova:repeatable ProductAttribute

with fields :

class ProductAttribute extends Repeatable
{
    public function fields(NovaRequest $request)
    {
        return [
            Text::make(__('Key'))->rules('required', 'max:255'),
            Textarea::make('Value')->rules('required', 'max:255'),
        ];
    }

and in app/Nova/Product.php in fields mehod :

HasMany::make(name: __('Product attributes'), attribute: 'productAttributes', resource: \App\Nova\ProductAttribute::class)->fullWidth(),

and having data :

What I see in Product attributes block :

Opening editor :

and when I try to saveentered data I got error :

[2024-11-22 10:51:09] local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'productAttributes' in 'field list' (Connection: mysql, SQL: update `product_attributes` set `productAttributes` = [{"type":"product-attribute","fields":{"key":"11","value":"11111"}}], `product_attributes`.`updated_at` = 2024-11-22 10:51:09 where `id` = 5) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'productAttributes' in 'field list' (Connection: mysql, SQL: update `product_attributes` set `productAttributes` = [{\"type\":\"product-attribute\",\"fields\":{\"key\":\"11\",\"value\":\"11111\"}}], `product_attributes`.`updated_at` = 2024-11-22 10:51:09 where `id` = 5) at /mnt/_work_sdb8/wwwroot/lar/NovaProducts/vendor/laravel/framework/src/Illuminate/Database/Connection.php:829)
[stacktrace]

What is wrong in my options ?

本文标签: laravel novaWrong options in Repeatable component raised errorStack Overflow