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

mehdi_es's avatar

3 models and 2 pivot tables

I have 3 models :

class Customer extends Model
{
    // here is my question
}

class Vendor extends Model
{
     /**
     * The services of vendor.
     */
    public function services()
    {
        return $this->belongsToMany('App\Service');
    }
}

class Service extends Model
{
     /**
     * The services of vendor.
     */
    public function vendors()
    {
        return $this->belongsToMany('App\Vendor');
    }
}

so each vendor can have one or few services. also each service can belongs to one or more vendors ( Vendor - many to many - Service )

a customer can have one or more vendor services. so the Cusomer model has many to many relationship with the pivot table of Vendor-Service ( another pivot table between Customer and VendorService which is pivot table itself)

I want to have another pivot table (customer_vendorservices) which contains of customer_id and vendorservice_id what should I do ? using Pivot Model and call it VendorService? any other solutions ?

0 likes
1 reply
vipin93's avatar

use pivot table and also make relationships with customer with services and vendors and when u can call nested eager load

Please or to participate in this conversation.