Commodity.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. public function model():HasOne
  14. {
  15. return $this->hasOne(MaterialBoxModel::class,"commodity_id");
  16. }
  17. public function barcodes()
  18. {
  19. return $this->hasMany('\App\CommodityBarcode');
  20. }
  21. public function owner(){
  22. return $this->belongsTo('App\Owner','owner_id','id');
  23. }
  24. public function getBarcodeAttribute(){
  25. return $this->barcodes[0]['code']??'';
  26. }
  27. public function getOwnerNameAttribute(){
  28. return $this->owner['name']??'';
  29. }
  30. public function getOwnerCodeAttribute(){
  31. return $this->owner['code']??'';
  32. }
  33. public function setNameAttribute($value){
  34. $this->attributes['name']=str_replace(PHP_EOL,'',$value);
  35. }
  36. public function getNameAttribute($value){
  37. return str_replace(array("\r\n","\n","\r","\"","&quot;"),' ',$value);
  38. }
  39. public function newBarcode($barcode){
  40. $barcodeModel = $this->barcodes()->where('code', $barcode)->first();
  41. if(!$barcodeModel){
  42. return CommodityBarcode::query()->create(['commodity_id'=>$this['id'],'code'=>$barcode]);
  43. }
  44. return $barcodeModel;
  45. }
  46. }