i have (product, copoun) tables
relation betwen 2 table is
class Products extends Model
{
function Coupon(){
return $this->belongsTo('App\Models\Coupons');
}
}
when using API Resourdce to get products and copoun (if copoun_id ) have value, and copoun is active, i use this api resource
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'price'=> $this->price,
'coupon' => $this->coupon(function ($item) {
return [
'discount'=>$item->discount
];
})->where('active',1)
->where('begin_date', '<=', Carbon::now())
->where('end_date', '>=', Carbon::now())
->first()
];
}
and i get these values
{
"data": [
{
"id": 1,
"title": "تفاح",
"price": 5,
"coupon": {
"id": 1,
"vimg": "/images/copoun/apple.png",
"coupon_ar": "Soluta corrupti aut",
"coupon_en": null,
"discount": 50,
"active": 1,
"begin_date": "2021-03-22 00:00:00",
"end_date": "2021-08-12 00:00:00",
"admin_id": 1,
"created_at": "2021-07-12T12:46:24.000000Z",
"updated_at": "2021-07-12T21:53:21.000000Z",
"deleted_at": null
}
},
{
"id": 2,
"title": "بطيخ صيفي ع السكين",
"price": 10,
"coupon": null
}
]
}
my question how can i get ust discount from copoun , i dont need all values as (id, vimg,coupon_ar,coupon_en,active,begin_date.....)