Reputation: 365
I'm using Laravel 4.2. I have 2 models. I want to make a list of items with a button to delete the relationship.
My models:
Word {
public function posts()
{
return $this->belongsToMany('Post');
}
}
Post {
public function words() {
return $this->hasMany('Word');
}
}
My desired list for Word with id=1
When I click to any x button I want to delete the relationship (not the word nor post!).
My approach (blade):
@foreach ($word->posts as $post)
{{{ helper_delete_button_with($post->id) }}}
@endforeach
In conclusion, which is the best way to delete the relation?
Making a function to WordController with a route like /word/{$wordid}/{$postid} (so I have to have a hidden input with $wordid)?
Making a controller like PageWordController with the destroy function (so I have to know the id of relationship, and I don't know how to get it using relationships)?
Any other solution?
Thank you.
Upvotes: 1
Views: 417
Reputation: 81
well, i think that you have 3 tables, one for posts, one for words and one for the relationship, in this case is better the second option because you only have to have the id of the relationship and destroy directly.
Upvotes: 1