Загрузка данных


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderPart extends Model
{
    use HasFactory;

    protected $table = 'order_parts';

    protected $fillable = [
        'repair_order_id', 'part_id', 'quantity', 'price_at_time'
    ];

    protected static function booted()
    {
        static::saved(function ($orderPart) {
            $orderPart->repairOrder->updatePartsCost();
        });

        static::deleted(function ($orderPart) {
            $orderPart->repairOrder->updatePartsCost();
        });
    }

    public function repairOrder()
    {
        return $this->belongsTo(RepairOrder::class);
    }

    public function part()
    {
        return $this->belongsTo(Part::class);
    }
}