kanashin
kanashin

Reputation: 365

Laravel: Delete relationships

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?

Thank you.

Upvotes: 1

Views: 417

Answers (1)

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

Related Questions