How can i get data by using "with" laravel relationship function by a condtion?

  eloquent, eloquent--relationship, laravel, php

this is my table => table image

//This is my controller
public function listUserStripeActionableRequests(Request $request) {
        return $model = UserRequest::whereNull('deleted_at')
            ->with('oldPackage')
            ->get();
}


userRequest.php

public function oldPackage() {
    return $this->hasOne(Package::class, 'id', 'old_package_id');
}

I need to get data according to package_type in the table
if package_type == 1 then i need to get data from Package::class,
if package_type == 2 then from AddOnsPackage::class then model function like :-
$this->hasOne(AddOnsPackage::class, 'id', 'old_package_id');

How can i use single query to get data accordingly?

Source: Ask PHP

LEAVE A COMMENT