Abidus Sattar Aziz
Abidus Sattar Aziz

Reputation: 155

How to get all data combining two models in laravel

I am using Eloquent and I want to get all data combining two models.

I have two models Courseoffer and Courseoffer_payment. Inside Courseoffer model I have this function:

public function payment()
{
    return $this->hasone('App\Models\Courseoffer_payment');
}

And this is my query :

return Courseoffer::findorfail(10)->payment;

This will give me data of Courseoffer_payment model associated with id 10 but I want all data for Courseoffer as well as Courseoffer_payment associated with id 10.

Upvotes: 1

Views: 192

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Try to use with instead like :

return Courseoffer::with('payment')->findorfail(10);

That will return the Courseoffer 10 instance with the payment information.

Upvotes: 1

Related Questions