This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

muhammad-ihsan
muhammad-ihsan

Hello, I am new in OctoberCMS. Just confused how to do BelongsToMany with Pivot Table, Let say i have a table relation like this:

And my Portfolio Model looks like :

<?php namespace Everoom\Portfolios\Models;

use Model;

/**
 * Portfolio Model
 */
class Portfolio extends Model
{
    use \October\Rain\Database\Traits\Sluggable;

    /**
     * @var array Generate slugs for these attributes.
     */
    protected $slugs = ['slug' => 'name'];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'everoom_portfolios_portfolios';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */
    public $hasOne = [];
    public $hasMany = [];
    public $belongsTo = [];
    public $belongsToMany = [
        'categories' => [
            'Everoom\Portfolios\Models\PortfolioCategory',
            'key'       => 'portfolio_id',
            'other_key' => 'id'
        ],

        'tools' => [
            'Everoom\Portfolios\Models\PortfolioTool',
            'key'       => 'portfolio_id',
            'other_key' => 'id'
        ]
    ];
    public $morphTo = [];
    public $morphOne = [];
    public $morphMany = [];
    public $attachOne = [];
    public $attachMany = [
        // 'pictures'  => 'System\Models\File'
    ];

}

and here is my config_relation.yaml

# ===================================
#  Relation Behavior Config
# ===================================

categories:
    label: Category
    view:
        list: $/everoom/portfolios/models/portfoliocategory/columns.yaml
    manage:
        list: $/everoom/portfolios/models/portfoliocategory/columns.yaml
    pivot:
        form: $/everoom/portfolios/models/portfoliocategory/fields.yaml

tools:
    label: Tool
    view:
        list: $/everoom/portfolios/models/portfoliotool/columns.yaml
    manage:
        list: $/everoom/portfolios/models/portfoliotool/columns.yaml
    pivot:
        form: $/everoom/portfolios/models/portfoliotool/fields.yaml

and it is return error like this:

Did i missing something? What should i do? Thank you

daftspunky
daftspunky

Might need something like this:

public $belongsToMany = [
    'categories' => [
        'Everoom\Portfolios\Models\PortfolioCategory',
        'table'     => 'everoom_portfolios_portfolio_categories',
        'key'       => 'portfolio_id',
        'otherKey'  => 'category_id'
    ],

    'tools' => [
        'Everoom\Portfolios\Models\PortfolioTool',
        'table'     => 'everoom_portfolios_portfolio_tools',
        'key'       => 'portfolio_id',
        'otherKey'  => 'tool_id'
    ]
];

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.