InnerDevice.php 2.3 KB

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