[Laravel] Relaties

Pagina: 1
Acties:

Vraag


Acties:
  • 0 Henk 'm!

  • Groax
  • Registratie: Oktober 2012
  • Laatst online: 02-10 11:33
Mijn vraag
Momenteel ben ik op mijn werk het Intranet aan het herbouwen. Deze gaat draaien in laravel 5.6/5.7
Dit omdat de huidige verouderd is (draait php 5.3................) dus update is wel een keertje nodig!

Nu heb ik een probleem met het verbinden van 3 tabellen.


fund_idrelation_idrelation_type_idcontact_fund


contact_fund kan 'f' of 'c' zijn. Doordat deze is gezet weet ik dat relation_id bij een contact of fund hoort. (raar!! ik weet het maar om dit tabel met 2.000 regels om te gooien is veel werk en andere software pakketten kunnen hierdoor kapot gaan)

dit is ook gedaan omdat de ID's van fund en contact het zelfde kunnen zijn..

Nu hoop ik dat iemand weet hoe ik vanuit een relatie een parent mee kan krijgen.. of dat ik dit met een WhereHas dit kan oplossen.


Relevante software en hardware die ik gebruik

Laravel 5.6
php 7.^


Wat ik al gevonden of geprobeerd heb

ooh ik heb heel veel geprobeerd..


PHP:
1
2
3
4
5
6
7
8
9
$view->relations = FundRelationXref::with('relationType', 'contacts', 'funds')
            ->where('fund_id', '=', $id)
            ->whereHas('contacts', function ($c) {
                $c->with('contacts');
                $c->where('contact_fund', '=', 'c');
                $c->where('status', 1);
            }, '=', 1)
            ->get()
            ->toArray();


PHP:
1
2
3
4
5
6
7
8
9
10
11
$view->relations = FundRelationXref::whereHas('funds', function ($f) use ($id) {
            [left][center][right][left][left][left]$f->where('fund_id', '=', $id);
            $f->where('contact_fund', '=', 'f');
        }, '=', 1)
            ->orWhereHas('contacts', function ($c) {
                $c->where('contact_fund', '=', 'c');
            }, '=', 1)
            ->where('fund_id', $id)
            ->with('contacts', 'funds', 'relationType')
            ->get()
            ->toArray();



Deze komt het dichtstbij! maar haal nog steeds beide op en niet alleen fund of contact..
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$view->relations = FundRelationXref::with('relationType', 'contacts', 'funds')
            ->where('fund_id', '=', $id)
            ->whereHas('contacts', function ($c) {
                $c->with('contacts');
                $c->where('contact_fund', '=', 'f');
                $c->where('status', 1);
            }, '=', 1)
            ->whereHas('funds', function ($c) {
                $c->with('contacts');
                $c->where('contact_fund', '=', 'c');
                $c->where('status', 1);
            }, '=', 1)
            ->get()
            ->toArray();


PHP:
1
2
3
4
5
6
7
8
9
10
11
12
array:1 [▼
  0 => array:12 [▼
    "ID" => 971
    "fund_id" => "1029"
    "relation_id" => "1039"
    "relation_type_id" => "11"
    "contact_fund" => "f"
    "contacts" => array:1 [▶]
    "funds" => array:1 [▶]
    "relation_type" => array:6 [▶]
  ]
]


Weet iemand de oplossing?????? je maakt mij erg blij!! zit al 3 uur te kopstoten... |:( |:( |:(

[ Voor 0% gewijzigd door Groax op 21-08-2018 11:21 . Reden: typo ]

Beste antwoord (via Groax op 21-08-2018 10:19)


  • ikvanwinsum
  • Registratie: Februari 2011
  • Laatst online: 05-10 18:06

ikvanwinsum

/dev/null

Welke tabellen zitten er in je database, en hoe verhouden die zicht tot elkaar?
Zover ik begrijp heb je:
-contacts
-funds
-relation types (???)

Opzich is het in Laravel wel mogelijk om een 'parent' relatie te hebben met verschillende soorten parents. Dit heet een polymorphic relation. Normaliter zou voor een polymorphic relation 2 kolommen in je db krijgen: parent_type en parent_id

U zegt: ‘Alles is toegestaan.’ Zeker, maar niet alles is goed. Alles is toegestaan, maar niet alles is opbouwend.

Alle reacties


Acties:
  • Beste antwoord
  • +1 Henk 'm!

  • ikvanwinsum
  • Registratie: Februari 2011
  • Laatst online: 05-10 18:06

ikvanwinsum

/dev/null

Welke tabellen zitten er in je database, en hoe verhouden die zicht tot elkaar?
Zover ik begrijp heb je:
-contacts
-funds
-relation types (???)

Opzich is het in Laravel wel mogelijk om een 'parent' relatie te hebben met verschillende soorten parents. Dit heet een polymorphic relation. Normaliter zou voor een polymorphic relation 2 kolommen in je db krijgen: parent_type en parent_id

U zegt: ‘Alles is toegestaan.’ Zeker, maar niet alles is goed. Alles is toegestaan, maar niet alles is opbouwend.


Acties:
  • 0 Henk 'm!

  • Groax
  • Registratie: Oktober 2012
  • Laatst online: 02-10 11:33
de relation types bevat de ID's van zowel fund en de contact table.

Ik ga kijken naar de polymorphic relation. THNX!

Acties:
  • 0 Henk 'm!

  • Groax
  • Registratie: Oktober 2012
  • Laatst online: 02-10 11:33
ikvanwinsum schreef op maandag 20 augustus 2018 @ 19:15:
Welke tabellen zitten er in je database, en hoe verhouden die zicht tot elkaar?
Zover ik begrijp heb je:
-contacts
-funds
-relation types (???)

Opzich is het in Laravel wel mogelijk om een 'parent' relatie te hebben met verschillende soorten parents. Dit heet een polymorphic relation. Normaliter zou voor een polymorphic relation 2 kolommen in je db krijgen: parent_type en parent_id
Ik heb dit geprobeerd en kom een heel eind!! helaas krijg ik geen informatie terug..

Wat ik voor nu heb:

PHP:
1
2
3
4
5
6
7
// AppServiceProvider
        Relation::morphMap([
            'f' => Fund::class,
            //'c' => Comment::class

            'c' => Contact::class
        ]);


PHP:
1
2
3
4
5
// FundRelationXref
    public function fundRelationTable()
    {
        return $this->morphTo(null, 'contact_fund', 'relation_id');
    }


PHP:
1
2
3
4
5
6
7
8
// Contact

    protected $morphClass = 'c';

    public function relations()
    {
        return $this->morphMany(FundRelationXref::class, 'fundRelationTable', 'contact_fund', 'relation_id');
    }


PHP:
1
2
3
4
5
6
7
8
// Fund

    protected $morphClass = 'f';

   public function relations()
    {
        return $this->morphOne(FundRelationXref::class, 'fundRelationXrefs', 'contact_fund', 'relation_id');
    }


Maar het haalt niks op. Dus ik run dit:

PHP:
1
2
        $view->test = FundRelationXref::find(478);
        dd($view->test->fundRelationTable);



Wat doe ik fout 8)7

Im a idiot...

Ik gebruite de verkeerde model.. Comment en dit moest Contact zijn.... :F

THNX! het werkt nu zoals het moet :)

[ Voor 5% gewijzigd door Groax op 21-08-2018 15:12 . Reden: tonto........ ]