<?php

namespace App\Models;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class Spec extends BaseModel
{
    public function device()
    {
        return $this->belongsTo('App\Models\Device', 'device_id');
    }

    public function device_name()
    {
        return $this->belongsTo('App\Models\DeviceName', 'device_name_id');
    }

    public function getValidator(Request $request, $type)
    {
        $validator = Validator::make($request->input('data'), [
            'name' => 'required'
        ], [
            'name.required' => '规格型号必填'
        ]);
        $validator->after(function ($validator) use($request) {
            if (!$request->input('device_id')) {
                $validator->errors()->add('device_id', '设备类型必填');
            }
            if (!$request->input('device_name_id')) {
                $validator->errors()->add('device_name_id', '设备名称必填');
            }
        });
        return $validator;
    }
}
