【问题标题】:Laravel 4.1 - $user->roles()->detach() issue in testing environmentLaravel 4.1 - 测试环境中的 $user->roles()->detach() 问题
【发布时间】:2014-04-01 03:47:14
【问题描述】:

我有表“用户”、“角色”和“用户角色”。当我使用 $user->roles()->attach() 和 detach() 时,它在我的本地环境中使用 MySQL 可以正常工作。但是当我尝试在我的测试环境(内存/Sqlite)中做同样的事情时,$user->roles()-detach() 不起作用。不会引发错误,它只是不会从数据透视表中删除条目。好像没有开火什么的。

我正在使用 PHPUnit 3.7

$this->roles()->attach(1);
$count = $this->roles->count();
$this->roles()->detach();

我也试过 $this->roles()->detach(1) 没有运气。有什么想法吗?

--- 更新---

我忘了提到的是我正在运行一个

$count1 = $user->roles->count();

在 attach() 和 detach() 之间,这似乎是导致问题的原因。为什么在 attach() 和 detach() 之间运行 count() 会导致 detach 不触发?我更新了上面的代码以反映我的新发现。

【问题讨论】:

    标签: php mysql testing laravel


    【解决方案1】:

    我想出了一个解决方案。而不是运行 $user->roles->count() 我将其切换为 $count1 = count(User::find($user->id)->roles->toArray());

    似乎工作正常,但我不知道是否有任何影响。

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 2018-08-11
      • 1970-01-01
      • 2018-11-23
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      相关资源
      最近更新 更多