Commodity.php 1.5 KB

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