Peter Peter - 4 years ago 375
PHP Question

Getting array of related IDs from a belongsToMany relation - Laravel 5.4

I had this solution working well in Laravel 5.3

$procedure = Procedure::findOrFail($id);
$attached_stages = $procedure->stages()->getRelatedIds()->toArray();


In my
Procedure
model:

public function stages()
{

return $this->belongsToMany('App\Models\Stage', 'procedure_stage', 'procedure_id', 'stage_id')->withPivot('id','status')->withTimestamps();
}


Now, after migrating to Laravel 5.4, I get this error:

Call to undefined method Illuminate\Database\Query\Builder::getRelatedIds()


Seems that the
getRelatedIds
has been removed.

My question:



how to get the array in 5.4?

Thank you in advance.

Answer Source

to get ids array you can use pluck function

$procedure->stages()->pluck('stages.id')->toArray();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download