Framework: Eloquent - Deleting records on a pivot table fails

Created on 2 Apr 2013  路  2Comments  路  Source: laravel/framework

There seems to be an issue with the generated query when trying to delete a record from a pivot table with Eloquent. Getting the follow error:

Connection->handleQueryException(object(PDOException), 'delete from `users` where `department_user`.`department_id` = ? and `deleted_at` is null', array('3')) in /Users/gstjohn/Dropbox/Projects/echo_api/vendor/laravel/framework/src/Illuminate/Database/Connection.php line 467

As you can see, it seems to be trying to run the delete on the related table ('users') rather than on the pivot table ('department_user').

The User model has a return $this->belongsToMany('Department')->whereNull('deleted_at')->withTimestamps(); within the departments() method.

The Department model has a return $this->belongsToMany('User')->whereNull('deleted_at')->withTimestamps(); within the users() method.

The line executing the delete is $this->users()->delete(); within the Department model's overridden delete() method.

Any advice or direction would be appreciated!

Most helpful comment

$this->users()->detach()

All 2 comments

$this->users()->detach()

Well, son of a... That's what I get for leaving my docs open for too long without refreshing on a dev branch :)

Thanks @taylorotwell!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainSauvaire picture RomainSauvaire  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

jackmu95 picture jackmu95  路  3Comments

iivanov2 picture iivanov2  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments