InnerDevice.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Validator;
  5. class InnerDevice extends BaseModel
  6. {
  7. protected $appends = ['inner_name'];
  8. public function workPoint()
  9. {
  10. return $this->belongsTo('App\Models\WorkPoint', 'work_point_id');
  11. }
  12. public function project()
  13. {
  14. return $this->belongsTo('App\Models\Project', 'project_id');
  15. }
  16. public function spec()
  17. {
  18. return $this->belongsTo('App\Models\Spec', 'spec_id');
  19. }
  20. public function device()
  21. {
  22. return $this->belongsTo('App\Models\Device', 'device_id');
  23. }
  24. public function device_name()
  25. {
  26. // return $this->belongsTo('App\Models\DeviceName', 'device_name_id');
  27. return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
  28. }
  29. public function devices_name(){
  30. return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
  31. }
  32. public function option()
  33. {
  34. return $this->belongsTo('App\Models\Option', 'status');
  35. }
  36. public function getValidator(Request $request, $type)
  37. {
  38. $validator = Validator::make($request->input('data'), [
  39. 'number' => 'required'
  40. ], [
  41. 'number.required' => '固定资产编号'
  42. ]);
  43. // $validator->after(function ($validator) use($request) {
  44. // if (!$request->input('device_name_id')) {
  45. // $validator->errors()->add('device_name_id', '设备名称必填');
  46. // }
  47. // if (!$request->input('spec_id')) {
  48. // $validator->errors()->add('spec_id', '规格型号必填');
  49. // }
  50. // });
  51. return $validator;
  52. }
  53. public function getInnerNameAttribute($value)
  54. {
  55. $id = $this->attributes['device_name_id'];
  56. return InnerDeviceNamesModel::where(['id'=>$id])->value('name');
  57. }
  58. public function getStatusLabel()
  59. {
  60. return $this['option'] ? '<div style="color: ' . $this['option']['color'] . '">' . $this['option']['name'] . '</div>' : '';
  61. }
  62. public function inner_device_names(){
  63. return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
  64. }
  65. }