Commit cc66b3a8 authored by Dan Jones's avatar Dan Jones Committed by Jens Segers

Get base query before update so that scopes are applied (#1799)

Fixes #1798
parent b9cc872a
......@@ -44,7 +44,7 @@ class Builder extends EloquentBuilder
return 1;
}
return $this->query->update($this->addUpdatedAtColumn($values), $options);
return $this->toBase()->update($this->addUpdatedAtColumn($values), $options);
}
/**
......
......@@ -21,6 +21,7 @@ class QueryTest extends TestCase
public function tearDown(): void
{
User::truncate();
Scoped::truncate();
parent::tearDown();
}
......@@ -309,4 +310,21 @@ class QueryTest extends TestCase
$this->assertEquals(9, $results->total());
$this->assertEquals(1, $results->currentPage());
}
public function testUpdate()
{
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));
$this->assertEquals(1, User::where(['name' => 'Jim Morrison'])->count());
Scoped::create(['favorite' => true]);
Scoped::create(['favorite' => false]);
$this->assertCount(1, Scoped::get());
$this->assertEquals(1, Scoped::query()->update(['name' => 'Johnny']));
$this->assertCount(1, Scoped::withoutGlobalScopes()->where(['name' => 'Johnny'])->get());
$this->assertCount(2, Scoped::withoutGlobalScopes()->get());
$this->assertEquals(2, Scoped::withoutGlobalScopes()->update(['name' => 'Jimmy']));
$this->assertCount(2, Scoped::withoutGlobalScopes()->where(['name' => 'Jimmy'])->get());
}
}
<?php
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\Builder;
class Scoped extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'scoped';
protected $fillable = ['name', 'favorite'];
protected static function boot()
{
parent::boot();
static::addGlobalScope('favorite', function (Builder $builder) {
$builder->where('favorite', true);
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment