Comments (4)
Thanks for the idea! Sounds like something that could be done.
If you have time, could you explain your use case in a bit more detail?
from laravel-has-many-merged.
I have one model "Profile" with two foreign keys: "admin_id" and "manager_id" that point to the same table "Person". It also has one connected User.
// Profile Model
/**
* @return BelongsTo
*/
public function user(): BelongsTo {
return $this->belongsTo(User::class, 'user_id');
}
/**
* @return BelongsTo
*/
public function admin(): BelongsTo {
return $this->belongsTo(Person::class, 'admin_id');
}
/**
* @return BelongsTo
*/
public function manager(): BelongsTo {
return $this->belongsTo(Person::class, 'manager_id');
}
I have two relations in Person model:
// Person model
/**
* @return HasOne
*/
public function adminProfile(): HasOne {
return $this->hasOne(Profilo::class,'admin_id');
}
/**
* @return HasOne
*/
public function managerProfile(): HasOne {
return $this->hasOne(Profilo::class,'manager_id');
}
Now i want to access the User from the Person (one Person can have only one adminProfile or one managerProfile, not both of them). Actually I use this accessor:
// Person model
/**
* Get connected User
* @return mixed
*/
public function getUserAttribute(): mixed {
$profile = ($this->adminProfile ?? $this->managerProfile); // first not null
return $profile->user;
}
But with the attribute I cannot use eager loading, and I would like to have a relation like your "hasManyMerged":
/**
* @return HasOneMerged
*/
public function profile(): HasOneMerged
{
return $this->hasOneMerged(Profile::class, ['admin_id', 'manager_id'])
->oldestOfMany();
}
"oldestOfMany()" is used to get only one record.
Another simpler example is here
from laravel-has-many-merged.
@Hirishiolo Please check out if !13 resolves your problem. It is currently not perfect and I'll add more tests, but I think it is stable enough to test the use case.
from laravel-has-many-merged.
thank you and very nice work! But unfortunately I have php 8.0 in my project, so I cannot use it. I plan to upgrade php in the next future, so I will try your branch!
Thanks!
from laravel-has-many-merged.
Related Issues (9)
- JSON columns HOT 5
- withCount doesn't work HOT 3
- Inverse relationship HOT 5
- Laravel 10 support HOT 1
- I'm a bit confused! HOT 1
- Trying to install with the "^0" for Laravel 8 but getting error HOT 3
- Order/Sort parent based on Childrens HOT 2
- merging two relation BelongsToMany and hasMany and returning relation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-has-many-merged.