The
optional function accepts any argument and allows you to access properties or call methods on that object. If the given object is null, properties and methods will return null instead of causing an error:<?php return optional($user->address)->street;
{!! old('name', optional($user)->name) !!}
<?php protected function getCurrentOwner()
{
return optional($this->connection->table($this->table)->where('key', $this->name)->first())->owner;
}
<?php $this->cachedRoot = null;
$this->cachedScheme = null;
tap(optional($this->routeGenerator)->defaultParameters ?: [], function ($defaults) {
$this->routeGenerator = null;
if (! empty($defaults)) {
<?php public function testOptional()
{
$this->assertNull(optional(null)->something());
$this->assertEquals(10, optional(new class
{
<?php {
$this->assertNull(optional(null)->something());
$this->assertEquals(10, optional(new class
{
public function something()
{
return 10;
}
})->something());
}
public function testOptionalWithCallback()
<?php public function testOptionalWithCallback()
{
$this->assertNull(optional(null, function () {
throw new RuntimeException(
'The optional callback should not be called for null'
);
}));
$this->assertEquals(10, optional(5, function ($number) {
return $number * 2;
<?php );
}));
$this->assertEquals(10, optional(5, function ($number) {
return $number * 2;
}));
}
public function testOptionalWithArray()
<?php public function testOptionalWithArray()
{
$this->assertSame('here', optional(['present' => 'here'])['present']);
$this->assertNull(optional(null)['missing']);
$this->assertNull(optional(['present' => 'here'])->missing);
}
<?php public function testOptionalWithArray()
{
$this->assertSame('here', optional(['present' => 'here'])['present']);
$this->assertNull(optional(null)['missing']);
$this->assertNull(optional(['present' => 'here'])->missing);
}
<?php {
$this->assertSame('here', optional(['present' => 'here'])['present']);
$this->assertNull(optional(null)['missing']);
$this->assertNull(optional(['present' => 'here'])->missing);
}
public function testOptionalReturnsObjectPropertyOrNull()
<?php public function testOptionalReturnsObjectPropertyOrNull()
{
$this->assertSame('bar', optional((object) ['foo' => 'bar'])->foo);
$this->assertNull(optional(['foo' => 'bar'])->foo);
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);
}
<?php public function testOptionalReturnsObjectPropertyOrNull()
{
$this->assertSame('bar', optional((object) ['foo' => 'bar'])->foo);
$this->assertNull(optional(['foo' => 'bar'])->foo);
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);
}
<?php {
$this->assertSame('bar', optional((object) ['foo' => 'bar'])->foo);
$this->assertNull(optional(['foo' => 'bar'])->foo);
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);
}
public function testOptionalDeterminesWhetherKeyIsSet()
<?php public function testOptionalDeterminesWhetherKeyIsSet()
{
$this->assertTrue(isset(optional(['foo' => 'bar'])['foo']));
$this->assertFalse(isset(optional(['foo' => 'bar'])['bar']));
$this->assertFalse(isset(optional()['bar']));
}
<?php public function testOptionalDeterminesWhetherKeyIsSet()
{
$this->assertTrue(isset(optional(['foo' => 'bar'])['foo']));
$this->assertFalse(isset(optional(['foo' => 'bar'])['bar']));
$this->assertFalse(isset(optional()['bar']));
}
<?php {
$this->assertTrue(isset(optional(['foo' => 'bar'])['foo']));
$this->assertFalse(isset(optional(['foo' => 'bar'])['bar']));
$this->assertFalse(isset(optional()['bar']));
}
public function testOptionalAllowsToSetKey()