Forráskód Böngészése

edit catalog: dont show delete btn on products with relations

Alexander Musikhin 7 hónapja
szülő
commit
0808f45982

+ 19 - 0
app/Models/Product.php

@@ -7,7 +7,9 @@ use App\Models\Scopes\YearScope;
 use Illuminate\Database\Eloquent\Attributes\ScopedBy;
 use Illuminate\Database\Eloquent\Casts\Attribute;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Support\Facades\DB;
 
@@ -112,6 +114,23 @@ class Product extends Model
         return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
     }
 
+    public function products_sku(): HasOne
+    {
+        return $this->hasOne(ProductSku::class, 'product_id', 'id');
+    }
+
+    public function hasRelations(): bool
+    {
+        if($this->products_sku && ($this->products_sku->count() > 0)) {
+            return true;
+        }
+
+        if($this->orders && ($this->orders->count() > 0)) {
+            return true;
+        }
+        return false;
+    }
+
 
 
 }

+ 1 - 1
database/migrations/2025_04_05_231749_create_maf_orders_table.php

@@ -16,7 +16,7 @@ return new class extends Migration
             $table->unsignedInteger('year');
             $table->string('order_number')->nullable();
             $table->foreignId('user_id')->constrained('users');
-            $table->foreignId('product_id')->constrained('products');
+            $table->foreignId('product_id')->constrained('products')->restrictOnDelete();
             $table->unsignedInteger('quantity');
             $table->unsignedInteger('in_stock');
             $table->timestamps();

+ 1 - 1
resources/views/catalog/edit.blade.php

@@ -36,7 +36,7 @@
                         </div>
                     </div>
                     <div class="col-12">
-                        @include('partials.submit', ['deleteDisabled' => (!isset($product)), 'offset' => 6, 'delete' => ['form_id' => 'deleteProduct']])
+                        @include('partials.submit', ['deleteDisabled' => (!isset($product) || $product->hasRelations()), 'offset' => 6, 'delete' => ['form_id' => 'deleteProduct']])
                     </div>
                 </div>