ethar's avatar
Level 5

2 foriegn id from same table

i have products table, i want admin when add product, can choose similar product.

i created new pivot table called similar_products with 2 field

    public function up(): void
    {
        Schema::create('similar_products', function (Blueprint $table) {
            $table->foreignId('product_id')->constrained()->cascadeOnDelete();
            $table->integer('similar_id')->references('products')->cascadeOnDelete();
        });
    }

my question how must second field name mast be, i uses similar_id , but i thinks this is not correct, and how can use realtion ship in Product Model

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To create a pivot table that holds relationships between products and similar products, you can indeed use a table like similar_products. However, you need to ensure that both product_id and similar_id are foreign keys that reference the id on the products table. Here's how you can define the migration for the similar_products table:

public function up(): void
{
    Schema::create('similar_products', function (Blueprint $table) {
        $table->unsignedBigInteger('product_id');
        $table->unsignedBigInteger('similar_product_id');

        $table->foreign('product_id')->references('id')->on('products')->cascadeOnDelete();
        $table->foreign('similar_product_id')->references('id')->on('products')->cascadeOnDelete();

        $table->primary(['product_id', 'similar_product_id']); // Composite primary key
    });
}

In this migration, I've renamed similar_id to similar_product_id for clarity, and I've set up both columns as foreign keys that reference the id column on the products table. I've also added a composite primary key to ensure that each combination of product_id and similar_product_id is unique.

Now, let's define the relationship in the Product model. Since a product can have multiple similar products and a similar product can also be related to multiple products, this is a many-to-many relationship. Here's how you can define it in the Product model:

class Product extends Model
{
    // ...

    public function similarProducts()
    {
        return $this->belongsToMany(Product::class, 'similar_products', 'product_id', 'similar_product_id');
    }

    public function relatedToProducts()
    {
        return $this->belongsToMany(Product::class, 'similar_products', 'similar_product_id', 'product_id');
    }
}

In this model, I've defined two relationships:

  1. similarProducts - This relationship finds other products that are similar to the current product.
  2. relatedToProducts - This relationship finds products for which the current product is listed as similar.

With these relationships, you can now easily attach and detach similar products to a given product. Here's an example of how you might use these relationships:

$product = Product::find(1);
$similarProduct = Product::find(2);

// Attach a similar product
$product->similarProducts()->attach($similarProduct->id);

// Detach a similar product
$product->similarProducts()->detach($similarProduct->id);

Remember to replace 1 and 2 with the actual product IDs you want to relate. This code assumes that you have two products with IDs 1 and 2, and you want to mark them as similar to each other.

Please or to participate in this conversation.