问题
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.
回答1:
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');
}
}
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 andrelated_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
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.
来源:https://stackoverflow.com/questions/52961104/october-cms-relation-to-self