Reputation: 39
I want to delete a row in pivot table, but nothing changes, so can anyone help me on this case? I have pivot table like this:
public function up()
{
Schema::create('group_list', function(Blueprint $table)
{
$table->integer('group_id');
$table->integer('id');
$table->primary(['group_id','id']);
});
}
My first model:
class AddressBook extends Model
{
public function addressbookgroups(){
return $this->belongsToMany(AddressBookGroup::class, 'group_list', 'id', 'group_id');
}
...
}
My second model:
class AddressBookGroup extends Model
{
public function addressbooks(){
return $this->belongsToMany(AddressBook::class, 'group_list', 'group_id','id');
}
}
In index blade I want to delete a row from AddressBook blade:
....
<form action="{{ route('group-list.destroy', $addressbook->id) }}" method="POST">
@csrf
@method('delete')
<button type="submit" onclick="return confirm('Are you sure you want to delete this?')"
class="btn btn-danger">Delete</button>
</form>
And finally my method for delete:
class AddressBookGroupListController extends Controller
{
public function destroy(AddressBook $address_book)
{
$address_book->delete();
return redirect('address-book-group-list');
}
}
Upvotes: 0
Views: 208
Reputation: 1776
If you want to delete an entry from the pivot table, you can do it with the help of relations.
$address_book = AddressBook::find($id);
Delete entries from pivot table formed from combination of above $address_book
and the address_book_group
with the id as $address_book_group_id
$address_book->addressBookGroups->detach($address_book_group_id);
Delete all pivot table entries associated with $address_book
$address_book->addressBookGroups->detach();
Upvotes: 1