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

romulo27's avatar

Relationship with 3 tables

I'm confusing a relationship with more than 3 tables. I have the DealershipStore,DealershipsStoreBrand and DealershipsStoreCategoryOptions models. As soon as I paste data into DealershipStore, I store that id in theDealershipsStoreBrand table and so on DealershipsStoreCategoryOptions, but for that I need a sync. The problem is failing to relate the sync between them.

class DealershipStore extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function dealershipStoreBrands(): BelongsToMany
    {
        return $this->belongsToMany(DealershipsStoreBrand::class);
            // ->with(BrandDealerships::class)
            // ->with(Brand::class);
    }
}
class DealershipsStoreBrand extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function dealershipStore()
    {
        return $this->belongsTo(DealershipStore::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function dealershipBrand()
    {
        return $this->belongsTo(BrandDealerships::class);
    }
}
class DealershipsStoreCategoryOptions extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function dealershipStoreBrand()
    {
        return $this->belongsTo(DealershipsStoreBrand::class);
    }

    public function brandStoreOption()
    {
        return $this->belongsTo(BrandStoreCategoryOption::class);
    }

}
0 likes
2 replies
audunru's avatar

Can you please explain how these relationships work in real life. Eg. “A store can have many brands”. Since you’re having trouble with your code it’s difficult to understand what you’re trying to achieve from the code alone.

Please or to participate in this conversation.