GithubHelp home page GithubHelp logo

Comments (4)

korridor avatar korridor commented on June 19, 2024

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.

Hirishiolo avatar Hirishiolo commented on June 19, 2024

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.

korridor avatar korridor commented on June 19, 2024

@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.

Hirishiolo avatar Hirishiolo commented on June 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.