F
F
freecode2015-05-25 02:44:30
Laravel
freecode, 2015-05-25 02:44:30

How to use composite keys in Laravel relationships?

I use composite keys in related tables -

Schema::create('order_details', function (Blueprint $table) {
            $table->integer('order_id');
            $table->integer('product_id');
            $table->decimal('price')->unsigned();

            $table->primary(['order_id', 'product_id']);
        });

Schema::create('order_details_services', function (Blueprint $table) {
            $table->integer('order_id');
            $table->integer('product_id');
            $table->integer('service_id');
            $table->decimal('price')->unsigned();

            $table->primary(['order_id', 'product_id', 'service_id']);
        });

An order with details is linked by order_id, but products with services are already linked by a composite key [order_id, product_id]. How to get services for each product in an order via hasMany relationship in Laravel?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question