How to delete records with relationship with multiple tables

Posted 3 months ago by AbdulBazith

Guys my project has a purchase order form.

i have two tables purchase_order_table and order_list_table

both in relationship purchase_order_table is parent tble and order_list is child table.

this is my purchase_order_migration file

 Schema::create('purchase_orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('login_user_id')->unsigned();
            $table->foreign('login_user_id')->references('id')->on('users');
            $table->string('pur_order_num');
            $table->integer('supplier_id')->unsigned();
            $table->foreign('supplier_id')->references('id')->on('suppliers');
            $table->date('order_date');
            $table->time('order_time');
            $table->date('expec_date')->nullable();
            $table->time('expec_time')->nullable();
            $table->date('del_date')->nullable();
            $table->time('del_time')->nullable();
            $table->timestamps();
        });

this is my order_list migration file

  Schema::create('order_lists', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('login_user_id')->unsigned();
            $table->foreign('login_user_id')->references('id')->on('users');
            $table->integer('purchase_order_id')->unsigned();
            $table->foreign('purchase_order_id')->references('id')->on('purchase_orders');
            $table->integer('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('products');
            $table->bigInteger('qty');
            $table->timestamps();
        });

i didnt use the onDelete('cascade'); in the migration.

now i need to delete record from parent table which in turn must delete all the relevant child table.

how to do this??

i need few calculation also.. those are in other tables.

say for example.

if i add a product to store, say rice 40 kg 4000 rupees then it will be added to the store and also the amount will be updated to that supplier in supplier_amount table.

so when i delete the entry which i made in the store then it should less that 4000 rupees amount amount from the supplier_amount table and also the qnty from the store table.

so how i can i delete a record with relationship with multiple tables.

Kindly some one suggest your ideas please.

Please sign in or create an account to participate in this conversation.