1

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.

Occam's chainsaw
  • 189
  • 3
  • 14

1 Answers1

4

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_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.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • Thank you so much, Hardik. Thanks to your clear and complete answer I got a great working solution for a similar problem! I appreciate it so much! – chocolata Feb 23 '20 at 14:53
  • A quick follow-up; I'm having trouble outputting the related events on my page. When I dd() I get an output of null every time. Has something changed? – chocolata Feb 23 '20 at 17:19