3

hope you're having a good day.

I'm using Laravel 8. I have three models and I need those models "entangled", so to speak.

So, I have three basic tables

areas,threats,positions
---
id
name

So the relationship needed is something like this:

  • Every t3 belongsToMany t1 and vice versa. (Many to Many)
  • Each t3.t1 relationship belongsToMany t2 (Many to Many)

My approach so far is this:

  • For the first relationship I have a belongsToMany realtionship defined on my models (t3.t1).
  • For the second relationship, I have created a custom pivot model for the pivot table, in that model I defined the second many to many relationship (t3t1.t2).

So far, the first relationship can be saved by doing $model->relatedModel()->attach($id);.

Now, for the second relationship, how can I attach the related models?

My last resort is to query the saved custom pivot model and attach the t2 model(s), but I wanted to ask first if there's a cleaner, eloquent-laravel way to do this.

Any advice would help. Thanks in advance for taking your time.

chuysbz
  • 1,262
  • 6
  • 18
  • 47

2 Answers2

1

explanation

the attach method is actually a Model function. so with your withPivot t3.t1 is not a model yet , when you are accessing with pivot magic method from your relation belongs to many it only return the column

answers

so for your situation, withPivot t3.t1 pivot as Model instance. here the steps

  1. create new PivotModel that extends use Illuminate\Database\Eloquent\Relations\Pivot;
  2. on the your t1Model, add using($classNamespace) to the belongsToMany method, example: belongsToMany()->using(PivotModel::class)
  3. then when t1->getT2s->pivot is already returning Model instance and you can use attach function to that pivot
david valentino
  • 920
  • 11
  • 18
1

What I can think is 2 way one is just refer Spatie Laravel Role permission

In Spatie the relationship is like Many-to-many relationship between Permission and Role and Many-to-Many Polymorphic relationship mapping User, Role and Permission.

OR

Using extra attributes in many-to-many pivot relation.

somethinglike

return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');

and attach and detach also accepts extra attributes.

$user->roles()->attach($roleId, ['expires' => $expires]);

while retrieving you can use where clause to specific your choice

return $this->belongsToMany('App\Role')->wherePivot('approved', 1);

I hope this will give you enough idea to implement it. all the snippet are from documentation, so you can directly search and refer

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • I actually implemented it with pivot columns like you answer. I was wondering if there could be a more "eloquent" way to do it, maybe the spatie approach is more likely to work. Thank you. – chuysbz Sep 27 '20 at 22:48
  • @Chuy Why you think, this is not eloquent? I would like to understand that, so that I could think about how you want to achieve it? – Prafulla Kumar Sahu Sep 27 '20 at 22:50
  • @Chuy another question I have is, what is your thoughts on implementing the spate way? – Prafulla Kumar Sahu Sep 27 '20 at 22:50
  • When I say "eloquent" I mean using a declarative way to extract information (as spatie permissions handles it). Kind regards. – chuysbz Sep 27 '20 at 22:54
  • @Chuy okay, so do you think if you implement spate way, your issue will be resolved? if yes, do you want me to explain that? – Prafulla Kumar Sahu Sep 27 '20 at 22:55
  • I don't know, I'm gonna try the answer by David Valentino first which is is more similar to what I already had. – chuysbz Sep 27 '20 at 22:57
  • @Chuy okay, try which ever is suitable to you, I am adding reference to Spatie GitHub models, so that you can take reference if required and also you can ask me question if required. – Prafulla Kumar Sahu Sep 27 '20 at 23:11
  • @Chuy what is the status of your issue, is there anything I can help? – Prafulla Kumar Sahu Sep 29 '20 at 07:17