48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
use App\Models\itemTransaksi;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
class Item extends Model
|
|
{
|
|
use HasFactory;
|
|
protected $table = 'items';
|
|
|
|
protected $fillable = [
|
|
'id_produk',
|
|
'id_nampan',
|
|
'kode_item',
|
|
];
|
|
|
|
protected $hidden = ['created_at', 'updated_at', 'deleted_at'];
|
|
|
|
// ✅ Auto-generate kode_item setiap kali create
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::created(function ($item) {
|
|
if (!$item->kode_item || $item->kode_item === 'belum pak') {
|
|
$prefix = "TMJC";
|
|
$date = $item->created_at->format('Ymd');
|
|
$number = str_pad($item->id, 4, '0', STR_PAD_LEFT);
|
|
|
|
$item->kode_item = $prefix . $date . $number;
|
|
$item->save();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function produk()
|
|
{
|
|
return $this->belongsTo(Produk::class, 'id_produk');
|
|
}
|
|
|
|
public function nampan()
|
|
{
|
|
return $this->belongsTo(Nampan::class, 'id_nampan');
|
|
}
|
|
}
|