Skip to content

Commit e351423

Browse files
committed
version
2 parents 7a7582f + baec6c2 commit e351423

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ trait GuardsAttributes
2727
*/
2828
protected static $unguarded = false;
2929

30+
/**
31+
* The actual columns that exist on the database and can be guarded.
32+
*
33+
* @var array
34+
*/
35+
protected static $guardableColumns = [];
36+
3037
/**
3138
* Get the fillable attributes for the model.
3239
*
@@ -190,12 +197,30 @@ public function isFillable($key)
190197
*/
191198
public function isGuarded($key)
192199
{
193-
if (strpos($key, '->') !== false) {
194-
$key = Str::before($key, '->');
200+
if (empty($this->getGuarded())) {
201+
return false;
195202
}
196203

197204
return $this->getGuarded() == ['*'] ||
198-
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded()));
205+
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded())) ||
206+
! $this->isGuardableColumn($key);
207+
}
208+
209+
/**
210+
* Determine if the given column is a valid, guardable column.
211+
*
212+
* @param string $key
213+
* @return bool
214+
*/
215+
protected function isGuardableColumn($key)
216+
{
217+
if (! isset(static::$guardableColumns[get_class($this)])) {
218+
static::$guardableColumns[get_class($this)] = $this->getConnection()
219+
->getSchemaBuilder()
220+
->getColumnListing($this->getTable());
221+
}
222+
223+
return in_array($key, static::$guardableColumns[get_class($this)]);
199224
}
200225

201226
/**

src/Illuminate/Foundation/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Application extends Container implements ApplicationContract, CachesConfig
3333
*
3434
* @var string
3535
*/
36-
const VERSION = '7.23.2';
36+
const VERSION = '7.23.3';
3737

3838
/**
3939
* The base path for the Laravel installation.

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Contracts\Events\Dispatcher;
1212
use Illuminate\Database\Connection;
1313
use Illuminate\Database\ConnectionResolverInterface;
14+
use Illuminate\Database\ConnectionResolverInterface as Resolver;
1415
use Illuminate\Database\Eloquent\Builder;
1516
use Illuminate\Database\Eloquent\Collection;
1617
use Illuminate\Database\Eloquent\JsonEncodingException;
@@ -1096,11 +1097,21 @@ public function testUnderscorePropertiesAreNotFilled()
10961097
public function testGuarded()
10971098
{
10981099
$model = new EloquentModelStub;
1100+
1101+
EloquentModelStub::setConnectionResolver($resolver = m::mock(Resolver::class));
1102+
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(stdClass::class));
1103+
$connection->shouldReceive('getSchemaBuilder->getColumnListing')->andReturn(['name', 'age', 'foo']);
1104+
10991105
$model->guard(['name', 'age']);
11001106
$model->fill(['name' => 'foo', 'age' => 'bar', 'foo' => 'bar']);
11011107
$this->assertFalse(isset($model->name));
11021108
$this->assertFalse(isset($model->age));
11031109
$this->assertSame('bar', $model->foo);
1110+
1111+
$model = new EloquentModelStub;
1112+
$model->guard(['name', 'age']);
1113+
$model->fill(['Foo' => 'bar']);
1114+
$this->assertFalse(isset($model->Foo));
11041115
}
11051116

11061117
public function testFillableOverridesGuarded()
@@ -2314,7 +2325,7 @@ public function getDates()
23142325
class EloquentModelSaveStub extends Model
23152326
{
23162327
protected $table = 'save_stub';
2317-
protected $guarded = ['id'];
2328+
protected $guarded = [];
23182329

23192330
public function save(array $options = [])
23202331
{

0 commit comments

Comments
 (0)