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. // }
  30. public function devices_name(){
  31. return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
  32. }
  33. public function option()
  34. {
  35. return $this->belongsTo('App\Models\Option', 'status');
  36. }
  37. public function getValidator(Request $request, $type)
  38. {
  39. $validator = Validator::make($request->input('data'), [
  40. 'number' => 'required'
  41. ], [
  42. 'number.required' => '固定资产编号'
  43. ]);
  44. // $validator->after(function ($validator) use($request) {
  45. // if (!$request->input('device_name_id')) {
  46. // $validator->errors()->add('device_name_id', '设备名称必填');
  47. // }
  48. // if (!$request->input('spec_id')) {
  49. // $validator->errors()->add('spec_id', '规格型号必填');
  50. // }
  51. // });
  52. return $validator;
  53. }
  54. public function getInnerNameAttribute($value)
  55. {
  56. $id = $this->attributes['device_name_id'];
  57. return InnerDeviceNamesModel::where(['id'=>$id])->value('name');
  58. }
  59. public function getStatusLabel()
  60. {
  61. return $this['option'] ? '<div style="color: ' . $this['option']['color'] . '">' . $this['option']['name'] . '</div>' : '';
  62. }
  63. public function inner_device_names(){
  64. return $this->hasOne(InnerDeviceNamesModel::class,'id','device_name_id');
  65. }
  66. }