I am having a Relation manager in a controller. I am wondering how to get the newly created id in
onRelationManageCreate
Scenario:
After creating a model record i want to display that record in a popup immediatly after it is created.
But i am struggling to get the id of the newly created relation model.
you can listen to the event attached to the relation in the model. Example:
YourModel::extend(function ($model) {
$model->bindEvent('model.relation.attach', function (string $relationName, array $ids, array $attributes) {
// check here the value of $relationName and grab the ids
}
})
public function boot()
{
CmsBlock::extend(function ($model) {
$model->bindEvent('model.relation.attach', function (string $relationName, array $ids, array $attributes) {
// check here the value of $relationName and grab the ids
trace_log("New relation {$relationName} was created", $ids);
});
});
}
The parent model Model is belongsToMany to CmsBlock
However still not listeneing to the event. is there anythign i am doing wrong?
public function boot()
{
ParentModel::extend(function ($model) {
$model->bindEvent('model.relation.attach', function (string $relationName, array $ids, array $attributes) {
// check here the value of $relationName and grab the ids
trace_log("New relation {$relationName} was created", $ids);
if ($relationName == 'nameOfYourRelationship' // for example, blocks) {
// grab the new id here
}
});
});
}
@chris thanks alot. it works. Can i ask how you sent it to javascript responce. e.g: when the relation is created the ajax sent some details of the newly created realtion without the id. this way we get the id but my question is that how can i get this id in the javascript responce?