0
votes

I'm trying to delete a product that has 6 relationships as follow:

  • brand->belongsTo(Brand::class);
  • tags->belongsToMany(Tag::class, 'tags_product');
  • fields->belongsToMany(Field::class, 'product_field');
  • sizes->belongsToMany(Size::class, 'product_size');
  • countries->belongsToMany(Country::class, 'country_product');
  • countryProducts->hasMany(CountryProduct::class);
  • productFields->hasMany(ProductField::class);
  • exportationFactors->hasMany(ExportationFactor::class);

Do I have to get rid of those relations first or not necessarily?

Currently I have this code for the delete function

ini_set('max_execution_time', 300);
$products = $request->all();

try {
    DB::beginTransaction();
    foreach ($products as $product) {
        $dbProduct = $this->getProduct($product['SKU']);
        $dbProduct->brand()->delete();
        $dbProduct->tags()->delete();
        $dbProduct->fields()->delete();
        $dbProduct->sizes()->delete();
        $dbProduct->countries()->delete();
        $dbProduct->exportationFactors()->delete();

        Log::error($dbProduct);
        $dbProduct->delete();
    }

    DB::commit();
} catch (Exception $e) {
    DB::rollBack();
    throw new HttpException(500, 'Sucedio un error eliminando la información favor intentar de nuevo');
}

And I get this error

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (sondel.products, CONSTRAINT products_brand_id_foreign FOREIGN KEY (brand_id) REFERENCES brands (id)) (SQL: delete from brands where brands.id = 2)

2
You have to delete the rows in the other table that are linked to this table BEFORE you can delete the parent row. OR set up a CONSTRAINT to cascade deletesRiggsFolly
@RiggsFolly how would I delete the rows first?Nancy
I think its presumed that you would either a) know or b) have access to the database's schema so you would use that information to see in which tables foreign keys are.parttimeturtle

2 Answers

0
votes

Try this:

$dbProduct->delete();
Brand::where('product_id', $dbProduct->id)->delete();
Tags::where('product_id', $dbProduct->id)->delete();

and add all the tables

0
votes

This is the solution I arrived to:

ini_set('max_execution_time', 300);
        $products = $request->all();
        try {
            DB::beginTransaction();
            foreach ($products as $product) {
                $product = $this->getProduct($product['SKU']);
                $product->sizes()->detach();
                $product->tags()->detach();
                $product->fields()->detach();
                $product->countries()->detach();
                $product->exportationFactors()->delete();
                $product->delete();
                DB::commit();
            }

            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();
            throw new HttpException(500, 'Sucedio un error eliminando la información favor intentar de nuevo');
        }

and in vue.js

sendData(data, index) {
                this.loading = true;
                this.error = {};
                this.$http.post(this.baseUrl, data, this.params)
                    .then(
                        () => {
                            this.successAction();
                            this.statuses[index] = 1;
                            this.errorDis = false;

                        },
                        (res) => {
                            this.showErrors(res);
                            this.statuses[index] = 2;
                            this.errorDis = true;
                        }
                    );
            },