从表主中删除数据具有多对多关系

时间:2018-09-01 04:11:23

标签: database laravel

我将laravel框架用于后端。

我有4张桌子:

1。产品

2。产品颜色

3。图片

4。 Image_product_color

我要删除表格上的数据:图片和Image_product_color 我尝试删除此数据时,可以将其从image_product_color中删除,但是无法从表Images中删除数据。有什么问题吗?

这是我的数据库架构:

1。产品

Schema::create('products', function (Blueprint $table) {
  $table->increments('id');
  $table->string('product_code')->unique();
  $table->text('name');
  $table->text('slug');
  $table->bigInteger('base_price');
  $table->integer('weight')->nullable();
  $table->text('description')->nullable();
  $table->smallInteger('status')->default(0);
  $table->timestamps();
});

2。产品颜色

Schema::create('product_colors', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('product_id')->unsigned();
      $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
      $table->string('base_color');
      $table->string('original_color');
      $table->timestamps();
    });

3。图片

Schema::create('images', function (Blueprint $table) {
    $table->increments('id');
    $table->binary('image');
    $table->timestamps();
});

4。 Image_product_color

Schema::create('image_product_color', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('product_color_id')->unsigned();
  $table->foreign('product_color_id')->references('id')->on('product_colors')->onDelete('cascade');
  $table->integer('image_id')->unsigned();
  $table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
  $table->timestamps();
});

模型:

ProductColor

class ProductColor extends Model
{
  protected $fillable=[
    'product_id','base_color','original_color'
  ];

  public function products()
  {
    return $this->belongsTo(Product::class,'product_id');
  }
  public function images()
  {
    return $this->belongsToMany(Image::class);
  }

  public function productsizes()
  {
    return $this->hasMany(ProductSize::class);
  }

}

图片

class Image extends Model
{
    protected $fillable=[
      'image'
    ];

    public function categories()
    {
      return $this->belongsToMany(Category::class);
    }
    public function productcolors()
    {
      return $this->belongsToMany(ProductColor::class);
    }
}

用雄辩的方法iam:

这是我的代码:

  $productcolor = ProductColor::findOrFail($id);
        $total_product_variant = Productcolor::where('product_id',$productcolor->product_id)->get();
        if (count($total_product_variant) === 1) {
          Product::destroy($productcolor->product_id);
        }
        if (count($productcolor->images) > 0) {
          foreach ($productcolor->images as $images) {
              $url = public_path().'/upload/products/'.$images->image;
              if (file_exists($url)) {
                unlink($url);
                $productcolor->images()->detach($images->id);
                Image::destroy($images->id);
                // Image::delete($images->id);
              }else {
                $productcolor->images()->detach($images->id);
                Image::destroy($images->id);
                // Image::delete($images->id);
              }
          }
        }

        ProductColor::destroy($id);
        return response()->json([
          'status'=>'success'
        ]);

    }

1 个答案:

答案 0 :(得分:0)

您输入id图像时出错:Image::destroy($images->id);在数据透视表中使用image_id $table->integer('image_id')->unsigned();命名图像ID

然后在本节中:Image::destroy($images->id);应该是:Image::destroy($images->image_id);

如果您使用的是$images->id,则您删除了错误的数据