statamic-rad-pack / runway

Eloquently manage your database models in Statamic.
https://statamic.com/addons/rad-pack/runway
MIT License
113 stars 49 forks source link

Field value not visible while attaching related resource #588

Closed LukaBis closed 1 month ago

LukaBis commented 2 months ago

Description

I have 2 resources: ArticleCategory and Article. They have many to many relationship. Article belongs to System, which is another resource. In article table page (where all the articles are listed), I have a system column that displays system id, it is visible and it works as you can see on the image below.

articles_table

On single article category page, there is articles field. I can click on 'select' and choose articles I want to attach from article list. Problem is that on that list system id is not visible. I also tried to display system name, but it did not work.

system_not_visible

Below I provide you with relevant migrations, models, blueprints.

article migrations

        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('article_number')->unique();
            $table->string('unit_number');
            $table->foreign('system_id')->references('id')->on('systems')->onDelete('restrict');
            $table->text('description_long');
            $table->text('description_short');
            $table->integer('parts_per_unit');
            $table->decimal('part_price_factor');
            $table->string('price_type');
            $table->decimal('price');
            $table->string('currency');
            $table->string('image_url');
            $table->timestamps();
            $table->softDeletes();
        });

systems migration/schema

Schema::create('systems', function (Blueprint $table) {
            $table->unsignedInteger('id')->primary();
            $table->string('name');
            $table->string('image')->nullable();
            $table->timestamps();
        });

article categories

Schema::create('article_categories', function (Blueprint $table) {
            $table->id();
            $table->string('title_de')->unique();
            $table->string('title_fr')->nullable()->unique();
            $table->timestamps();
        });

+also create a pivot table for many to many relationship between article categories and articles

Article model

class Article extends Model
{
    /** @use HasFactory<ArticleFactory> */
    use HasFactory, HasRunwayResource, SoftDeletes;

    protected $guarded = [];

    /**
     * @return BelongsTo<System, Article>
     */
    public function system(): BelongsTo
    {
        return $this->belongsTo(System::class, 'system_id');
    }

    /**
     * @return BelongsToMany<ArticleCategory>
     */
    public function articleCategories(): BelongsToMany
    {
        return $this->belongsToMany(ArticleCategory::class, 'article_article_category', 'article_id', 'article_category_id');
    }

...
}

System model

class System extends Model
{
    use HasRunwayResource;

    protected $guarded = [];

    /**
     * @return Attribute<string, never>
     */
    protected function systemIdentifier(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->id,
            set: fn (int $value) => [
                'id' => $value,
            ],
        );
    }

    /**
     * @return HasMany<Product>
     */
    public function products(): HasMany
    {
        return $this->hasMany(Product::class);
    }

    /**
     * @return HasMany<Article>
     */
    public function articles(): HasMany
    {
        return $this->hasMany(Article::class);
    }
}

Article category model

class ArticleCategory extends Model
{
    use HasRunwayResource, LocalisedPropertyAccessorTrait;

    protected $guarded = [];

    /**
     * @return Attribute<string, string>
     */
    protected function title(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->getLocalizedProperty('title'),
        );
    }

    /**
     * @return BelongsToMany<Article>
     */
    public function articles(): BelongsToMany
    {
        return $this->belongsToMany(Article::class, 'article_article_category', 'article_category_id', 'article_id');
    }

    /**
     * @return BelongsToMany<ProductType>
     */
    public function productTypes(): BelongsToMany
    {
        return $this->belongsToMany(ProductType::class, 'article_category_product_type', 'article_category_id', 'product_type_id');
    }

    /**
     * This method is used for statamic to get the product types for HasMany fieldtype
     * as it only supports snake case naming.
     *
     * @return BelongsToMany<ProductType>
     */
    public function product_types(): BelongsToMany
    {
        return $this->productTypes();
    }
}

Article blueprint

tabs:
  main:
    display: Hauptteil
    sections:
      -
        fields:
          -
            handle: system_id
            field:
              relationship_name: system
              resource: system
              create: false
              type: belongs_to
              display: System
              visibility: read_only
              title_format: '{{ name }}'
              listable: true
          -
            handle: article_number
            field:
              type: text
              display: 'Artikel Nr.'
              visibility: read_only
              width: 25
              listable: true
          -
            handle: unit_number
            field:
              type: text
              display: 'Unit no.'
              visibility: read_only
              width: 25
              listable: true
          -
            handle: description_short
            field:
              type: text
              display: Name
              width: 50
              visibility: read_only
              listable: true
          -
            handle: description_long
            field:
              type: textarea
              display: Beschreibung
              visibility: read_only
              listable: true
          -
            handle: image_url
            field:
              type: link
              display: 'Bild (extern)'
              visibility: read_only
  price_package_unit:
    display: 'Preis & Verpackungseinheit'
    sections:
      -
        display: 'Neuer Abschnitt'
        fields:
          -
            handle: price_type
            field:
              type: text
              display: Preis-Typ
              visibility: read_only
              listable: true
          -
            handle: quantity_per_package_unit
            field:
              prepend: 'Pro Verpackungseinheit'
              append: Stück
              type: integer
              display: 'Anzahl pro Verpackungseinheit'
              width: 33
              visibility: read_only
          -
            handle: price_per_unit
            field:
              type: float
              display: 'Preis pro Verpackungseinheit'
              width: 33
              visibility: read_only
              listable: true
          -
            handle: currency
            field:
              type: text
              display: Währung
              width: 33
              hide_display: true
              visibility: read_only
          -
            handle: price_per_part
            field:
              type: float
              display: Einzelpreis
              visibility: computed
title: Article

Article category blueprint

tabs:
  main:
    display: Hauptteil
    sections:
      -
        fields:
          -
            handle: title_de
            field:
              type: text
              display: 'Title (de)'
              validate:
                - required
                - 'new App\Domains\Articles\Rules\UniqueArticleCategoryValue'
          -
            handle: title_fr
            field:
              type: text
              display: 'Title (fr)'
              validate:
                - 'new App\Domains\Articles\Rules\UniqueArticleCategoryValue'
          -
            handle: articles
            field:
              resource: article
              create: false
              type: has_many
              display: Articles
              title_format: '{{description_short}}'
          -
            handle: product_types
            field:
              resource: product_type
              create: false
              type: has_many
              display: 'Product Types'
          -
            handle: order
            field:
              type: integer
              display: Order
              instructions: 'Fitting steps will be sorted based on this number'
title: 'Article category'

System blueprint

tabs:
  main:
    display: Hauptteil
    sections:
      -
        fields:
          -
            handle: system_identifier
            field:
              type: integer
              display: 'System id'
              validate:
                - required
          -
            handle: name
            field:
              type: text
              display: Name
              instructions: 'System name'
              validate:
                - required
          -
            handle: image
            field:
              max_files: 1
              container: assets
              type: assets
              display: Image
              validate:
                - required
                - image
title: System

Steps to reproduce

  1. run migrations provided above (+1 pivot table)
  2. create models and register runway resources in runway config file
  3. create blueprints for each resource (provided above)
  4. try to attach article to article category and notice that system field is empty

Environment

Environment Application Name: Statamic Laravel Version: 11.19.0 PHP Version: 8.3.10 Composer Version: 2.7.8 Environment: local Debug Mode: ENABLED URL: localhost:8080 Maintenance Mode: OFF Timezone: Europe/Zurich Locale: de

Cache Config: NOT CACHED Events: NOT CACHED Routes: NOT CACHED Views: CACHED

Drivers Broadcasting: null Cache: file Database: sqlite Logs: stack / daily Mail: log Queue: sync Scout: collection Session: file

Locales Installed: de, en, fr LaravelLang\Lang\Plugin: 15.5.6 Locales Version: 2.9.2 Protected: de Publisher Version: 16.4.0

Sentry Enabled: MISSING DSN Environment: local Laravel SDK Version: 4.7.1 PHP SDK Version: 4.8.1 Release: NOT SET Sample Rate Errors: 100% Sample Rate Performance Monitoring: NOT SET Sample Rate Profiling: NOT SET Send Default PII: DISABLED

Statamic Addons: 3 Sites: 1 Stache Watcher: Disabled Static Caching: Disabled Version: 5.19.0 PRO

Statamic Addons statamic-rad-pack/runway: 7.7.3 statamic/eloquent-driver: 4.12.0 tv2regionerne/statamic-safeguard: 1.4.0

Statamic Eloquent Driver Asset Containers: file Assets: eloquent Blueprints: file Collection Trees: eloquent Collections: file Entries: eloquent Forms: file Global Sets: file Global Variables: eloquent Navigation Trees: eloquent Navigations: file Revisions: file Sites: file Taxonomies: file Terms: eloquent Tokens: eloquent

github-actions[bot] commented 1 month ago

Released as part of v7.9.0.