0

I am setting up a two database table, sale item and sale. When an admin make an inventory multiple sale item can added. After complete my sale inventory, i want to delete sale but i do not want to delete sale item table. My Sale Item migration table is:

public function up()
 {
    Schema::create('sales_items', function (Blueprint $table) {
        $table->increments('id');
        $table->string('product_name');
        $table->string('product_price');
        $table->string('product_quantity');
        $table->string('product_discount');
        $table->string('total_price');
        $table->integer('status');
        $table->integer('sale_id')->unsigned();
        $table->foreign('sale_id')->references('id')->on('sales')- >onDelete('cascade');
        $table->timestamps();
    });
}

Sale migration table is:

public function up()
{
    Schema::create('sales', function (Blueprint $table) {
        $table->increments('id');
        $table->string('sale_status');
        $table->string('total_price');
        $table->string('due');
        $table->timestamps();
    });
}

SaleItem Model :

public function sales(){
    return $this->hasOne(Sale::class, 'id', 'sale_id');
}

Sale Model:

public function saleitems(){
    return $this->hasMany(SalesItem::class, 'sale_id', 'id');
}

Now, How can i delete Sale table without delete sale item table?

omor faruk
  • 81
  • 1
  • 7
  • You should just soft delete by having an extra `deleted_at` column. – nice_dev Apr 30 '19 at 11:06
  • if you delete the parent then you will have orphaned child remaining unless you delete cascade. if you remove delete cascade then you can delete parent but it is an extremely bad practice i reckon – Saad Suri Apr 30 '19 at 11:35

0 Answers0