Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dakshhmehta's avatar

How to use Factory with many to many relations

Hello,

I am struggling to get the factory working with many to many relation. How do I use it with extra meta fields in pivot table that automatically gets populated.

The two models are Room and Booking

Thanks, Daksh

0 likes
1 reply
bobbybouwmann's avatar

What did you try? Can you show some code?

For extra fields you can do something like this:

class Carousel extends Model {

    public function slides()
    {
        return $this->belongsToMany('Slide', 'carousel_slides')
            ->withPivot(array('display_time', 'etc'));
    }

}

Please or to participate in this conversation.