| 1234567891011121314151617181920212223242526 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Commodity extends Model
- {
- protected $fillable=['name','sku','owner_id'];
- protected $appends=['barcode'];
- public function barcodes()
- {
- return $this->hasMany('\App\CommodityBarcode');
- }
- public function getBarcodeAttribute(){
- return $this->barcodes()->first()['code'];
- }
- public function newBarcode($barcode){
- $commodityBarcode=CommodityBarcode::where('commodity_id',$this['id'])->where('code',$barcode)->first();
- if(!$commodityBarcode){
- $commodityBarcode=new CommodityBarcode(['commodity_id'=>$this['id'],'code'=>$barcode]);
- $commodityBarcode->save();
- }
- }
- }
|