Skip to content

Иерархия для атрибутов #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ExsyDev opened this issue Mar 14, 2021 · 1 comment
Open

Иерархия для атрибутов #15

ExsyDev opened this issue Mar 14, 2021 · 1 comment

Comments

@ExsyDev
Copy link

ExsyDev commented Mar 14, 2021

Привет. Есть такой вопрос, у меня есть 2 модели Атрибутов, одна: Attribute, другая AttributeValue. Attribute: это модель для названия атрибутов, а AttributeValue это значения для данных атрибутов. Вопрос вот в чем, могу ли я в ресурсе подать эти атрибуты через этот пакет? Чтобы была иерархия как у категорий (Название атрибута -> значения ) (по типу как: главная категория -> дочерние) ? У меня в ресурсе продукта, в который я хочу добавить данную иерархию есть отношение:

public function attributes_values() { return $this->belongsToMany(AttributeValue::class, 'product_attribute_values'); }

А вот модель Attribute:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Attribute extends Model
{
    protected $guarded = [];

    public function attributeable()
    {
        return $this->morphTo();
    }

    public function categories()
    {
        return $this->morphedByMany(Category::class, 'attributeable');
    }

    public function values()
    {
        return $this->hasMany(AttributeValue::class);
    }

    public function getProductsExistsAttribute()
    {
        // dd($this->categories()->where('slug', request()->segment(2))->has('products')->whereHas()->exists());
        return $this->values()->has('products')->exists();
    }
}

Модель AttributeValue:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AttributeValue extends Model
{
    protected $guarded = [];

    protected $appends = ['products_exists'];

    public function attribute()
    {
        return $this->belongsTo(Attribute::class);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class, 'product_attribute_values');
    }

    public function getProductsExistsAttribute()
    {
        $category = request()->segment(2) ? \App\Category::where('slug', request()->segment(2))->first() : null;
        if($category) {
            return $this->products()->whereHas('categories', function($q) use ($category) { $q->whereIn('slug', $category->children->pluck('slug'))->orWhere('slug', $category->slug); })->exists();
        }
    }
}

В данный момент в ресурсе продукта я это делаю через SelectPlus пакет, в скобках пишу значение атрибута, чтобы было ясно, в продукте мне нужно выбирать именно значения атрибутов.

SelectPlus::make(__('Caracteristici'), 'attributes_values', \App\Nova\AttributeValue::class)->label(fn ($state) => $state->attribute->title . " <span class=\"text-danger\">({$state->value})</span>")->hideFromIndex()

Подскажите пожалуйста, как можно это сделать через nova nested tree attach many? Заранее спасибо.

@ExsyDev
Copy link
Author

ExsyDev commented Mar 16, 2021

Я попытался сделать так:

NestedTreeAttachManyField::make(__('Categories'), 'attributes_values', "\App\Nova\AttributeValue")->withLabelKey('value')

В итоге у меня получаются значения атрибутов: https://imgur.com/a/sGrHzhp но без родителей, мне нужно название атрибута и чтобы под ним были значения.

пытался делать и так тоже:

NestedTreeAttachManyField::make(__('Categories'), 'attributes_values', "\App\Nova\AttributeValue")->withIdKey('attribute_id')->withLabelKey('value')->withChildrenKey('id')

Тоже не работает. @phoenix-lib можешь помочь понять почему?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant