GithubHelp home page GithubHelp logo

Comments (5)

korridor avatar korridor commented on June 19, 2024

Hi @Agufi28,

thanks for your issue!
I don't really get your request. The inverse already exists, and it's a normal BelongsTo relation.
It's likely that I'm just misunderstanding your request.
Could you give me a simple real world example for this, maybe I can help you better then.

from laravel-has-many-merged.

Agufi28 avatar Agufi28 commented on June 19, 2024

Hi, thank you for your answer. I'll give you an example:

Model Children:

  • id
  • id_mother
  • id_father

Model Parent:

  • id
  • name

I need to get both parents from within the child model.

It would be something like this:

public function parents(){
    return $this->hasManyMerged(Parent::class, "id", ["id_mother","id_father"]);
}

The currents state of the library lets me do the relation from the Parent class. It's something like:

public function children(){
    return $this->hasManyMerged(Child::class, ["id_mother","id_father"]);
}

This is not the exact escenario where I need the relation because the real one is far too complicated to use as an example. But it's essentially the same. I need to get bot parents from within the child.

Thank you very much!

from laravel-has-many-merged.

korridor avatar korridor commented on June 19, 2024

Hi @Agufi28,

Ok, so for the example in the README the Message model would have a relation named something like participants, that returns a query with ->whereIn('id', [$sender, $receiver])?

Maybe something like this:

public function participants(): BelongsToMerged
{
    return $this->belongsToMerged(User::class, ['sender_user_id', 'receiver_user_id']);
}

If this is what you mean, then I think that this is a good addition to this package and I will add it when I find the time in the following weeks (hopefully not months).
If you want it earlier, or you are motivated, I would be open to PR! :)

from laravel-has-many-merged.

aeruggiero avatar aeruggiero commented on June 19, 2024

Hi @korridor,
first of all great library!

I'm using this thread because I think it's related to my issue:
I have a table users and a table friends.
The table friends has user_id_1 and user_id_2, plus some additional columns, but those first two are the one that relate two users.
With the HasManyMerged for a $user, I can get all the records in the friends table, no matter if the $user->id is the user_id_1 or the user_id_2 (and that's GREAT!).
But I would like to obtain also the other user info, something to use with the "with" relation, for example.
Let's say that

class User{
public function friends(){
return $this->hasManyMerged(Friend::class, ['user_id_1', 'user_id_2']);
}
}

I would like to know how I can get something like
$user->friends()->with('friendDetails'), in order to have a collection of users related to my $user through the friend table so that I can also perform additional queries on the friends like "where" (for example $user->friends()->with('friendDetails')->where('name', 'LIKE', '%something%').
Thanks!

from laravel-has-many-merged.

bmooreitul avatar bmooreitul commented on June 19, 2024

There is definitely a better way to do this... but my workaround using the existing code was to use the existing package classes and make new ones in the app/traits folder.

<?php

declare(strict_types=1);

namespace App\Traits;

use Illuminate\Database\Eloquent\Model;

trait BelongsManyMergedRelation
{
    /**
     * @param  class-string  $related
     * @param  string[]|null  $foreignKeys
     * @param  string|null  $localKey
     * @return BelongsManyMerged
     */
    public function belongsManyMerged(string $related, ?array $foreignKeys = null, ?string $localKey = null): BelongsManyMerged
    {
        $instance       = new $related();
        $localTable     = self::getTable();
        $actualLocalKey = $localTable.'.'.self::getKeyName();
        $localKey       = $localKey ?: $instance->getKeyName();
        $targetTable    = $instance->getTable();
        

        $foreignKeys = array_map(function ($foreignKey) use ($localTable) {
            return $localTable . '.' . $foreignKey;
        }, $foreignKeys);


        $newQuery = $instance->newQuery()->join($localTable, function($join) use($foreignKeys, $targetTable, $localKey){
            foreach($foreignKeys as $i => $foreignKey) $join->{($i >= 1 ? 'orOn' : 'on')}($targetTable.'.'.$localKey, '=', $foreignKey);             
        })->select($targetTable.'.*');

        return new belongsManyMerged($newQuery, $this, [$actualLocalKey], $localKey);
    }

    /**
     * Get the primary key for the model.
     *
     * @return string
     */
    abstract public function getKeyName();
}

Then I just added a BelongsManyMerged class in the app/Traits folder that was copy and paste of this package HasManyMerged class, while simply renaming the class and namespace.

<?php

declare(strict_types=1);

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
 * @template TRelatedModel of Model
 * @extends Relation<TRelatedModel>
 */
class BelongsManyMerged extends Relation
{
...

After doing this I was able to access the method ->belongsManyMerged(ParentClass, [array of local keys], parentLocalKey) exactly as expected including all query builder features.

For anyone else who is looking for a way to use this package for the inverse of a relationship.

Until this feature is added to the package, hopefully this helps someone out with inverse relationships.


Please note, I would've forked this branch and submitted a merge request to this repo, But I know this is just a workaround and there's a better way to do this.


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.