File: /mnt/data/ghayatcom/ghayatcom-api/app/LabOrderData.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class LabOrderData extends Model
{
use HasFactory, SoftDeletes;
protected $table = 'lab_order_data';
protected $fillable = [
'lab_order_id',
'member_id',
'item_id',
'type',
'item_name',
'parameter',
'mrp',
'price'
];
/**
* Relationship with LabOrder.
*/
public function labOrder()
{
return $this->belongsTo(LabOrder::class, 'lab_order_id');
}
/**
* Relationship with Member (User).
*/
public function member()
{
return $this->belongsTo(User::class, 'member_id'); // Assuming member is also a User
}
/**
* Relationship with LabParameter when type is 2.
*/
public function labParameter()
{
return $this->belongsTo(LabParameter::class, 'item_id');
}
/**
* Relationship with LabPackage when type is 1.
*/
public function labPackage()
{
return $this->belongsTo(LabPackage::class, 'item_id');
}
/**
* Optional: If you still want a generic item relationship based on type.
*/
public function item()
{
if ($this->type == 1) {
return $this->belongsTo(LabPackage::class, 'item_id');
} elseif ($this->type == 2) {
return $this->belongsTo(LabParameter::class, 'item_id');
}
return null;
}
}