<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DownloadProduct extends Model
{
    protected $table = 'download_product';

    protected $guarded = [];

    public static function attachProducts($downloadIds, $productId, $isAdd = false) 
    {
        if (!empty($downloadIds)) {
            $isAdd ?: self::where('product_id', $productId)->delete();
            foreach ($downloadIds as $val) {
                self::create(['download_id' => $val, 'product_id' => $productId]);
            }
            return true;
        } else {
            return $isAdd ? true : self::where('product_id', $productId)->delete();
        }
    }
}
