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


<?php

namespace App\Models;

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

class RepairOrder extends Model
{
    use HasFactory;

    protected $fillable = [
        'order_number', 'user_id', 'car_id', 'admin_id', 'status',
        'order_date', 'completion_date', 'problem_description', 
        'admin_notes', 'total_labor_cost', 'total_parts_cost', 'total_amount'
    ];

    protected $casts = [
        'order_date' => 'date',
        'completion_date' => 'date',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function car()
    {
        return $this->belongsTo(Car::class);
    }

    public function admin()
    {
        return $this->belongsTo(User::class, 'admin_id');
    }

    public function services()
    {
        return $this->belongsToMany(Service::class, 'order_services')
                    ->withPivot('quantity', 'price_at_time')
                    ->withTimestamps();
    }

    public function parts()
    {
        return $this->belongsToMany(Part::class, 'order_parts')
                    ->withPivot('quantity', 'price_at_time')
                    ->withTimestamps();
    }

    public function calculateTotal()
    {
        $this->total_amount = $this->total_labor_cost + $this->total_parts_cost;
        $this->saveQuietly();
        return $this->total_amount;
    }
}