| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelTimeFormat;
- use App\Traits\ModelLogChanging;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- class Commodity extends Model
- {
- use ModelLogChanging;
- use ModelTimeFormat;
- protected $fillable=['name','sku','owner_id','created_at','length',
- 'width','height','volumn',"type","pack_spec",'updated_at',"remark"];
- public function model():HasOne
- {
- return $this->hasOne(MaterialBoxModel::class,"commodity_id");
- }
- public function barcodes()
- {
- return $this->hasMany('\App\CommodityBarcode');
- }
- public function owner(){
- return $this->belongsTo('App\Owner','owner_id','id');
- }
- public function getBarcodeAttribute(){
- return $this->barcodes[0]['code']??'';
- }
- public function getOwnerNameAttribute(){
- return $this->owner['name']??'';
- }
- public function getOwnerCodeAttribute(){
- return $this->owner['code']??'';
- }
- public function setNameAttribute($value){
- $this->attributes['name']=str_replace(PHP_EOL,'',$value);
- }
- public function getNameAttribute($value){
- return str_replace(array("\r\n","\n","\r","\"","""),' ',$value);
- }
- public function newBarcode($barcode){
- $barcodeModel = $this->barcodes()->where('code', $barcode)->first();
- if(!$barcodeModel){
- return CommodityBarcode::query()->create(['commodity_id'=>$this['id'],'code'=>$barcode]);
- }
- return $barcodeModel;
- }
- }
|