Commodity.php 1.4 KB

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