Commodity.php 726 B

1234567891011121314151617181920212223242526
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Commodity extends Model
  5. {
  6. protected $fillable=['name','sku','owner_id'];
  7. protected $appends=['barcode'];
  8. public function barcodes()
  9. {
  10. return $this->hasMany('\App\CommodityBarcode');
  11. }
  12. public function getBarcodeAttribute(){
  13. return $this->barcodes()->first()['code'];
  14. }
  15. public function newBarcode($barcode){
  16. $commodityBarcode=CommodityBarcode::where('commodity_id',$this['id'])->where('code',$barcode)->first();
  17. if(!$commodityBarcode){
  18. $commodityBarcode=new CommodityBarcode(['commodity_id'=>$this['id'],'code'=>$barcode]);
  19. $commodityBarcode->save();
  20. }
  21. }
  22. }