鉴于雄辩的多对多关系:
迁移:
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');
});
}
}
型号:
class User extends Model
{
public function topics()
{
return $this->belongsToMany('App\Topic', 'topic_user');
}
}
我想知道如何删除给定用户的所有主题,目前我的代码如下:
Auth::user()->topics()->delete();
但我得到例外:
错误:表“topic_user”缺少FROM子句条目第1行:删除 来自“topic”的主题,其中“topic_user”。“user_id”= $ 1 ^(SQL:从中删除 “topic”,其中“topic_user”。“user_id”= 6)
我做错了什么?
答案 0 :(得分:1)
这很简单:
Auth::user()->topics()->detach();