Commodity.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Traits\ModelTimeFormat;
  5. use App\Traits\ModelLogChanging;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. class Commodity extends Model
  8. {
  9. use ModelLogChanging;
  10. use ModelTimeFormat;
  11. protected $fillable=['name','sku','owner_id','created_at','length',
  12. 'width','height','volumn',"type","pack_spec",'updated_at',"remark"];
  13. protected $appends=['barcode'];
  14. public function model():HasOne
  15. {
  16. return $this->hasOne(MaterialBoxModel::class,"commodity_id");
  17. }
  18. public function barcodes()
  19. {
  20. return $this->hasMany('\App\CommodityBarcode');
  21. }
  22. public function owner(){
  23. return $this->belongsTo('App\Owner','owner_id','id');
  24. }
  25. public function getBarcodeAttribute(){
  26. return $this->barcodes[0]['code']??'';
  27. }
  28. public function getOwnerNameAttribute(){
  29. return $this->owner['name']??'';
  30. }
  31. public function getOwnerCodeAttribute(){
  32. return $this->owner['code']??'';
  33. }
  34. public function setNameAttribute($value){
  35. $this->attributes['name']=str_replace(PHP_EOL,'',$value);
  36. }
  37. public function getNameAttribute($value){
  38. return str_replace(array("\r\n","\n","\r","\"","&quot;"),' ',$value);
  39. }
  40. public function newBarcode($barcode){
  41. $barcodeModel = $this->barcodes()->where('code', $barcode)->first();
  42. if(!$barcodeModel){
  43. return CommodityBarcode::query()->create(['commodity_id'=>$this['id'],'code'=>$barcode]);
  44. }
  45. return $barcodeModel;
  46. }
  47. }