vendredi 9 mars 2018

Laravel morphedByMany withPivots() the pivot attribute is empty

I have a problem with polymorph relation definition in laravel 5.5 eloquent. My pivot table name is contact_relations ( id, model_type, model_id, contact_id, valid_from ...). Accessing the Node-Model works but accessing the pivot attribute always returns NULL, this should be filled with the "valid_from".

$node->getRelations()['pivot']->valid_from works, but this is not the way described in document.

What I did wrong?

 class Contact extends Model
        {
        public function nodes()
            {
                return $this->morphedByMany('My\Models\Node', 'model', 'contact_relations')->withPivot('valid_from'); 

            }
        }

   class Node extends Model
        {
            public function contacts()
            {
                return $this->morphToMany('My\Models\Node', 'model', 'contact_relations')->withPivot('valid_from');
            }
        }

    $contact = Contact::findOrFail(2);

    foreach($contact->nodes as $node) {
       dd($node->pivot); // returns NULL
       dd($node->getRelations()['pivot']->valid_from); // return the valid_from
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2IfVWqE
via IFTTT

Aucun commentaire:

Enregistrer un commentaire