Support the ongoing development of Laravel.io →
posted 7 years ago
Eloquent

Hello, I have a relation like this :

A
- id
- fields
- X

B
- same id as A
- fields

C
- same id as A
- fields

So when we get a A item, following what's inside field X we can have a relation to B or C.

So I have two relation like this in A :

class A extends Eloquent {
// ...
	public function b()
	{
		return $this->hasOne(B::class, 'id');
	}

	public function c()
	{
		return $this->hasOne(C::class, 'id');
	}
}

I'm wondering if it's possible to do something that allow me to make one declaration like this

	public function bc()
	{
		return $this->relationOr('X=b', $this->hasOne(B::class, 'id'), 'X=c', $this->hasOne(C::class, 'id'));
	}

I supposed you got the idea. This kind of way to do it is extract from doctrine InheritanceType("JOINED") in the declaration of table. thanks

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Grummfy grummfy Joined 27 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.