Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Since a product_accessory_adaptor relates to a product and a catagory, why don't you create a ProductAccessoryAdaptor model with two belongs to relationships ?

Last updated 2 years ago.
0

hi pmall, the ProductAccessoryAdaptor model would not have a direct foreign key..Could you advise how is that doable? I've updated my question as well with regards on what i've achieved so far.

Last updated 2 years ago.
0

Why is this not suited ?

Schema::create('product_accessory_adaptor', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('product_id')->unsigned();
    $table->integer('accessory_id')->unsigned();
    // more fields...
    $table->foreign('product_id')->references('id')->on('accessories');
    $table->foreign('accessory_id')->references('id')->on('accessories');
}

then

class Adaptor extends Eloquent {

    protected $table = 'product_accessory_adaptor';

    public function products() {
        return $this->belongsTo('Product');
    }

    public function accessories() {
        return $this->belongsTo('Accessory');
    }
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

parn09 parn09 Joined 21 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.