Company-Contact Relationships in Laravel

This snippet demonstrates how to define relationships between companies and contacts using Eloquent in Laravel. The 'contacts' method establishes a many-to-many relationship, while 'companyContacts' defines a one-to-many relation with the CompanyContact model.
mail@pastecode.io avatar
unknown
php
24 days ago
415 B
5
Indexable
Never
/**
     *  The companies that belong to the contact
     */
    public function contacts(): BelongsToMany
    {
        return $this->belongsToMany(Contact::class)
            ->using(CompanyContact::class)
            ->withPivot('is_point_of_contact', 'roles')
            ->withTimestamps();
    }

    public function companyContacts(): HasMany
    {
        return $this->hasMany(CompanyContact::class);
    }
Leave a Comment