php - 删除与 Laravel 5 的一对多关系

标签 php laravel eloquent

我有一对多关系

在我的任务 Controller 中

public function todo()
{
  return $this->belongsTo('App\Todo');
}

在我的 Todo Controller 中

public function tasks()
{
  return $this->hasMany('App\Task');
}

使用以下代码添加关系

$todo = new Todo
$todo->save();
$task = new Task
$todo->tasks()->save($task);
$task->save();

但我想稍后删除它,而不是对象,只是关系

任何想法

最佳答案

this chapter of the docs :

When removing a belongsTo relationship, you may use the dissociate method. 
This method will reset the foreign key as well as the relation on the child model:

$user->account()->dissociate();
$user->save();

所以就你而言,

$task->todo()->dissociate();
$task->save();

关于php - 删除与 Laravel 5 的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31589709/

相关文章:

PHP 电商订单拆分给卖家 Laravel

php - 预加载 : Use `with` on pivot with eloquent relationship

php - 服务器未返回 SQL 数据

php - 斑马条纹 PHP MYSQL 表

jquery - 在 jQuery 中获取最接近的元素

javascript - Laravel 5 和 AngularJS 从 url 中删除 '#'

eloquent - Laravel Eloquent - orderBy 列在相关模型中查找

php - 在 PHP 脚本中 MySQL 查询返回 NULL 列

php - 在 PHP 中将数组元素移动到顶部

php - Laravel 查询 - 按计数排序