How to setup conditional relationship on Eloquent

Lets take a different approach in solving your problem. First lets setup relationship for the various models respectively. class User extends Model { public function agentProfile() { return $this->hasOne(AgentProfile::class); } public function institutionProfile() { return $this->hasOne(InstitutionProfile::class); } public function schoolProfile() { return $this->hasOne(SchoolProfile::class); } public function academyProfile() { return $this->hasOne(AcademyProfile::class); } // create scope to … Read more

Laravel merge relationships

Try out getter method for property which returns merged collections returned from relations. public function getCompetitionsAttribute($value) { // There two calls return collections // as defined in relations. $competitionsHome = $this->competitionsHome; $competitionsGuest = $this->competitionsGuest; // Merge collections and return single collection. return $competitionsHome->merge($competitionsGuest); } Or you can call additional methods before collection is returned to … Read more

Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

Let me answer step by step. 1. When do you need ` insertable = false, updatable = false`? Let’s look at the below mapping, public class Zip { @ManyToOne @JoinColumn(name = “country_code”, referencedColumnName = “iso_code”) private Country country = null @Column(name = “country_code”) private String countryCode; } Here we are referring to the same column … Read more