lundi 22 juillet 2019

Laravel 5.8 saving one to many relation on pivot table

I have a many to many relation with a pivot table. In this pivot table I have 2 one to many relations. It looks like this drawn out:

enter image description here

In code it looks like this:

WorkflowStep model:

public function workflowinstances(): BelongsToMany
{
    return $this->belongsToMany(WorkflowInstance::class)
        ->using(WorkflowInstanceWorkflowStep::class)
        ->withPivot([
            'started_at',
            'finished_at',
            'failed_at',
            'output',
        ])
        ->withTimestamps();
}

WorkflowInstance model:

public function workflowSteps(): BelongsToMany
{
    return $this->belongsToMany(WorkflowStep::class)
        ->using(WorkflowInstanceWorkflowStep::class)
        ->withPivot([
            'started_at',
            'finished_at',
            'failed_at',
            'output',
        ])
        ->withTimestamps();
}

WorkflowInstanceWorkflowStep Pivot model:

public function sourceFile(): BelongsTo
{
    return $this->belongsTo(File::class, 'source_file_id', 'id');
}

public function destinationFile(): BelongsTo
{
    return $this->belongsTo(File::class, 'destination_file_id', 'id');
}

File model:

public function workflowInstanceStepSources(): HasMany
{
    return $this->hasMany(WorkflowInstanceWorkflowStep::class, 'source_file_id', 'id');
}

public function workflowInstanceStepDestinations(): HasMany
{
    return $this->hasMany(WorkflowInstanceWorkflowStep::class, 'destination_file_id', 'id');
}

Now My goals is to save the file - pivot relation but this doesn't seem to be working:

$fileObject = $workflowStep->entity->sourceClient->files()->create([
    'name' => $filename,
    'extension' => $file['extension'],
    'category' => 'transfer',
    'original_file_id' => 1,
    'path' => $filePath,
    'link_id' => 1,
]);

$fileObject->workflowInstanceStepSources()->save();

I create a new File object and then I want to link it to the pivot table with my relation

Is what I am trying here even possible?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire