Trilateral (model) connection

I have the following schema and model with this seed data .

My goal is to achieve a triangular relationship, this may not be the right term, but finally what I call it.

In three directions:

  • Checks have tokens.
  • Attached icons have values โ€‹โ€‹that are known with the Cheque-> Token binding.

The problem is with the relation from Token to Value , where when I load Values they do not take into account the associated Cheque , therefore, returning all values โ€‹โ€‹belonging to Token .

I donโ€™t know if the Value schema is correct for this three-way relationship, so I also doubt that the correct Value relationships belonging to Cheque and Token are correct.

This is how I request models now (excerpt from the installation):

 Route::get('test', function() { $cheque = Cheq_Node::with(array('tokens' => function($query) { $query->where_sortable(1); }, 'tokens.values'))->first(); dd( $cheques ); }); 

I tried changing the link of Token values โ€‹โ€‹to:

 public function values() { return $this->has_many('Cheq_Value', 'token_id')->where_node_id($this->pivot->id); } 

But this produced me:

 Trying to get property of non-object 

Adding Log::dump( dump($this) ) before returning does not show the loaded model, but only the empty Eloquent model:

 object(Cheq_Token)[63] public 'attributes' => array (size=0) empty public 'original' => array (size=0) empty public 'relationships' => array (size=0) empty public 'exists' => boolean false public 'includes' => array (size=1) 'values' => null 

Not surprisingly, an error occurs here.

How do I make this triangular relationship?

+4
source share
2 answers

As a result, I switched with another Scheme and different Models .

It also required a different path for insertion, resulting in the following test seed .

Extracting everything:

 $nodes = Node::with(array('tokenlinks', 'tokenlinks.token', 'tokenlinks.values'))->get(); 
0
source

I think you need to look at the database schema using the appropriate foreign keys in order to get the correct data. Assuming you get the result you want, this is not always the best way.

Using joins in your database will do the trick ...

0
source

All Articles