0
votes

I have a simeple ManyToMany relation between 2 models when I try to access a field in my pivot table I get this error:

Trying to get property 'started_at' of non-object

I have followed the instructions on the laravel documentation correctly.

My Models:

Instance:

public function steps(): BelongsToMany
{
    return $this->belongsToMany(Step::class)
        ->using(InstanceStep::class)
        ->withTimestamps();
}

Step:

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

How I try to access 'started_at' of a step:

dd($step->pivot->started_at);

EDIT

private function finishStep(Step $step)
{
    dd($step->pivot->started_at);
}

/** @var Step $step */
foreach ($this->workflow->steps as $step) {
    StepJob::dispatch($step, $instance);
}

dd($step)

Step {#420 ▼
  #table: "workflow_steps"
  #fillable: array:6 [▶]
  #casts: array:3 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "id" => "ca646f59-b215-41b0-afe6-ea9e6174f4f5"
    "workflow_id" => "d786bd13-4111-4199-94d6-c52fef33b78b"
    "category" => "transport"
    "properties" => "props"
    "entity_type" => "App\TransportWorkflowStep"
    "entity_id" => "4d32e11c-6453-4f48-9419-7c5cbd647128"
    "order" => 1
    "created_at" => "2019-07-04 11:19:41"
    "updated_at" => "2019-07-04 11:41:16"
    "deleted_at" => null
  ]
  #original: array:10 [▶]
  #changes: []
  #dates: array:1 [▶]
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
  #forceDeleting: false
}

Attach step to instance

// set started_at
$workflowInstance->workflowsteps()->attach(
        $step,
        [
            'started_at' => Carbon::now(),
        ]
    ); 

// do something

// set finished_at when done
dd($step->pivot->started_at);

I did the dd($step->pivot->started_at); to see if i am working with the correct pivot row

2
Could you show how you fetch the $step?MyLibary
see edited OP with updated codemichael
I'm after the place you've used "find", "first" or whatever to fetch the record :)MyLibary
Could you dd($step) for us please?MyLibary
@MyLibary i edited OP with dd($step)michael

2 Answers

0
votes

You've not specified the relationship you're after, you may access the intermediate table using the pivot attribute on the models:

Try the following code:

private function finishStep(Step $step)
{
    foreach ($step->instances as $instance) {
        var_dump($instance->pivot->started_at);
    }

    die();
}
0
votes

It depends on how declare $step, it gets collection and you have to insert it in foreach loop.

private function finishStep(Step $step)
{
    dd($step->pivot->started_at);
}

this doesn't declare $step, you have to declare it first like

$step=Step::where(...)->get(); 

and then you can dd($step);

in short - object doesn't exist yet