Reputation: 54459
Given Eloquent Many To Many relationship:
Migration:
class Topics extends Migration
{
public function up()
{
Schema::create('topics', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('topic_user', function(Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('topic_id')->unsigned();
$table->foreign('topic_id')->references('id')->on('topics');
});
}
}
Model:
class User extends Model
{
public function topics()
{
return $this->belongsToMany('App\Topic', 'topic_user');
}
}
I'm wondering how can I delete all topics by given user, currently my code looks like this:
Auth::user()->topics()->delete();
But I'm getting exception:
ERROR: missing FROM-clause entry for table "topic_user" LINE 1: delete from "topics" where "topic_user"."user_id" = $1 ^ (SQL: delete from "topics" where "topic_user"."user_id" = 6)
What I'm doing wrong?
Upvotes: 0
Views: 124