Commodity.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelTimeFormat;
  6. class Commodity extends Model
  7. {
  8. use ModelTimeFormat;
  9. protected $fillable=['name','sku','owner_id','created_at','length','width','height','volumn',"type","pack"];
  10. protected $appends=['barcode'];
  11. // protected $appends=['barcode','owner_name','owner_code'];
  12. public function setNameAttribute($value){
  13. $this->attributes['name']=str_replace(PHP_EOL,'',$value);
  14. }
  15. public function getNameAttribute($value){
  16. return str_replace(array("\r\n","\n","\r","\"","&quot;"),' ',$value);
  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 newBarcode($barcode){
  35. $barcodeModel = $this->barcodes()->where('code', $barcode)->first();
  36. if(!$barcodeModel){
  37. return CommodityBarcode::query()->create(['commodity_id'=>$this['id'],'code'=>$barcode]);
  38. }
  39. return $barcodeModel;
  40. }
  41. }