Skip to content

Commit

Permalink
[11.x] Prevent blank Helper from Serializing Eloquent Models (#53911)
Browse files Browse the repository at this point in the history
* Prevent jsonSerialize model blank helper

* styleci
  • Loading branch information
SanderMuller authored Dec 16, 2024
1 parent 603b0cf commit d288758
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Contracts\Support\DeferringDisplayableValue;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Env;
use Illuminate\Support\Fluent;
Expand Down Expand Up @@ -61,6 +62,10 @@ function blank($value)
return false;
}

if ($value instanceof Model) {
return false;
}

if ($value instanceof Countable) {
return count($value) === 0;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Countable;
use Error;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Env;
use Illuminate\Support\Optional;
use Illuminate\Support\Sleep;
Expand Down Expand Up @@ -75,6 +76,19 @@ public function testBlank()
$this->assertTrue(blank($object));
}

public function testBlankDoesntJsonSerializeModels()
{
$model = new class extends Model
{
public function jsonSerialize(): mixed
{
throw new RuntimeException('Model should not be serialized');
}
};

$this->assertFalse(blank($model));
}

public function testClassBasename()
{
$this->assertSame('Baz', class_basename('Foo\Bar\Baz'));
Expand Down

0 comments on commit d288758

Please sign in to comment.