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.
how to get the array in 5.4?
Thank you in advance.
It was removed(basically, changed the name, nothing more) from 5.4, but you have it in another name as i looked deep inside the belongToMany.php
file. Use this
And it should work very nicely.
$attached_stages = $procedure->stages()->allRelatedIds()->toArray();
Hope this help you, and other who will face that problem in the future and look in this post.
to get ids array you can use pluck function
$procedure->stages()->pluck('stages.id')->toArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With