Ad

October CMS Relation To Self

- 1 answer

I have the Event model and want to be able to select other events in it to display on the frontend.

The first thing that I tried is the relations, but the problem is I don't know how to use the pivot table because actually, it should contain two fields and both should be named as 'event_id' which is obviously impossible. Maybe someone can suggest any way to avoid this or a better way to solve my problem with events choice field in Event model?

Thanks in advance.

Ad

Answer

yes same name for relation_id could be impossible, BUT we have option :)

you can create intermediate table like this

<?php namespace HardikSatasiya\SoTest\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class BuilderTableCreateHardiksatasiyaSotestEventsEvents extends Migration
{
    public function up()
    {
        Schema::create('hardiksatasiya_sotest_events_events', function($table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->integer('events_id')->unsigned();
            $table->integer('related_events_id')->unsigned();
        });
    }

    public function down()
    {
        Schema::dropIfExists('hardiksatasiya_sotest_events_events');
    }
}

enter image description here

Then, just we need to add relation with proper options

public $belongsToMany =[
    'skills' => [
        'HardikSatasiya\SoTest\Models\Skills',
        'table' => 'hardiksatasiya_sotest_events_skills',
        'order' => 'title'
    ],
    'related_evst\Models\Skills',
        'table' => 'hardiksatasiya_sotest_events_skills',
        'order' => 'title'
    ],
    'related_events' => [
        'HardikSatasiya\SoTest\Models\Events',
        'table' => 'hardiksatasiya_sotest_events_events',
        'order' => 'title',
        'key' => 'events_id',
        'otherKey' => 'related_events_id'
    ]
];

we are specifying that for this relation event_id is for main event and related_events_id is for other event which we add as related event. config_relation.yaml <- which will be inside event controller view folder.

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

skills:
    label: Skill
    view:
        list: $/hardiksatasiya/sotest/models/skills/columns.yaml
        toolbarButtons: add|remove
    manage:
        list: $/hardiksatasiya/sotest/models/skills/columns.yaml
        form: $/hardiksatasiya/sotest/models/skills/fields.yaml

related_events:
    label: Events
    view:
        list: $/hardiksatasiya/sotest/models/events/columns.yaml
        toolbarButtons: add|remove
    manage:
        list: $/hardiksatasiya/sotest/models/events/columns.yaml
        form: $/hardiksatasiya/sotest/models/events/fields.yaml

event modal fields $/hardiksatasiya/sotest/models/events/fields.yaml

fields:
    title:
        label: Title
        span: auto
        type: text
    description:
        label: Description
        size: ''
        span: auto
        type: textarea
    skills:
        label: Skills
        type: partial
        path: $/hardiksatasiya/sotest/controllers/events/_relation_skills.htm
    related_events:
        label: Events
        type: partial
        path: $/hardiksatasiya/sotest/controllers/events/_related_events.htm

now for partial _related_events.htm

<?= $this->relationRender('related_events') ?>

result

enter image description here

accessing in code

use HardikSatasiya\SoTest\Models\Events as EventsModel;

function onStart() {
    $this['event'] = $event = EventsModel::first();
    dd($event->related_events);
}

if any doubt please comment.

Ad
source: stackoverflow.com
Ad