当我更新关系时,例如更新Child上的parent_id(Child belongsTo Parent,Parent hasMany Child)并使用Child-> Parent对象进行响应,不知何故返回的Parent仍然是旧的.我认为这是因为当时已经加载了Parent.
我现在想要取消关系,以便再次从db中获取新的Parent.
有没有办法卸载加载的关系?就像你可以通过使用model-> load('relation')来延迟加载,你还可以再次卸载它吗?
非常感谢!
我在使用全局范围时遇到问题,尤其是删除范围.
在我的用户模型中,我有一个ActivatedUsersTrait,即引入了一个全球范围内只查询与列用户"激活"设置为true(电子邮件验证之后,用户被"激活").
到目前为止一切正常,当我查询User :: all()时,我只获得具有activated = true的用户.
我现在的问题是,如何将未激活的用户包含在我的查询中,就像SoftDeletingTrait通过withTrashed()一样?这只与我的ActivationController有关,我需要获取User,设置activate = true并将它们保存回db.
我在ActiveUsersTrait中创建了一个withInactive()方法,基于我在SoftDeletingTrait中找到的方法,但是当我在User :: withInactive-> get()上运行查询时,未激活的用户将不会显示在结果.
这是我的ActiveUsersTrait:
use PB\Scopes\ActiveUsersScope;
trait ActiveUsersTrait {
public static function bootActiveUsersTrait()
{
static::addGlobalScope(new ActiveUsersScope);
}
public static function withInactive()
{
// dd(new static);
return (new static)->newQueryWithoutScope(new ActiveUsersScope);
}
public function getActivatedColumn()
{
return 'activated';
}
public function getQualifiedActivatedColumn()
{
return $this->getTable().'.'.$this->getActivatedColumn();
}
}
Run Code Online (Sandbox Code Playgroud)
和我的ActiveUsersScope:
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;
class ActiveUsersScope implements ScopeInterface {
public function apply(Builder $builder)
{
$model = $builder->getModel();
$builder->where($model->getQualifiedActivatedColumn(), true);
}
public function remove(Builder …
Run Code Online (Sandbox Code Playgroud)