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:
return optional($user->address)->street;

{!! 
old('name'optional($user)->name) !!}
    protected function getCurrentOwner()

    {

        return 
optional($this->connection->table($this->table)->where('key'$this->name)->first())->owner;

    }
        $this->cachedRoot null;

        
$this->cachedScheme null;



        
tap(optional($this->routeGenerator)->defaultParameters ?: [], function ($defaults) {

            
$this->routeGenerator null;



            if (! empty(
$defaults)) {
    public function testOptional()

    {

        
$this->assertNull(optional(null)->something());



        
$this->assertEquals(10optional(new class

        {
    {

        
$this->assertNull(optional(null)->something());



        
$this->assertEquals(10optional(new class

        {

            public function 
something()

            {

                return 
10;

            }

        })->
something());

    }



    public function 
testOptionalWithCallback()
    public function testOptionalWithCallback()

    {

        
$this->assertNull(optional(null, function () {

            throw new 
RuntimeException(

                
'The optional callback should not be called for null'

            
);

        }));



        
$this->assertEquals(10optional(5, function ($number) {

            return 
$number 2;
            );

        }));



        
$this->assertEquals(10optional(5, function ($number) {

            return 
$number 2;

        }));

    }



    public function 
testOptionalWithArray()
    public function testOptionalWithArray()

    {

        
$this->assertSame('here'optional(['present' => 'here'])['present']);

        
$this->assertNull(optional(null)['missing']);

        
$this->assertNull(optional(['present' => 'here'])->missing);

    }
    public function testOptionalWithArray()

    {

        
$this->assertSame('here'optional(['present' => 'here'])['present']);

        
$this->assertNull(optional(null)['missing']);

        
$this->assertNull(optional(['present' => 'here'])->missing);

    }
    {

        
$this->assertSame('here'optional(['present' => 'here'])['present']);

        
$this->assertNull(optional(null)['missing']);

        
$this->assertNull(optional(['present' => 'here'])->missing);

    }



    public function 
testOptionalReturnsObjectPropertyOrNull()
    public function testOptionalReturnsObjectPropertyOrNull()

    {

        
$this->assertSame('bar'optional((object) ['foo' => 'bar'])->foo);

        
$this->assertNull(optional(['foo' => 'bar'])->foo);

        
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);

    }
    public function testOptionalReturnsObjectPropertyOrNull()

    {

        
$this->assertSame('bar'optional((object) ['foo' => 'bar'])->foo);

        
$this->assertNull(optional(['foo' => 'bar'])->foo);

        
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);

    }
    {

        
$this->assertSame('bar'optional((object) ['foo' => 'bar'])->foo);

        
$this->assertNull(optional(['foo' => 'bar'])->foo);

        
$this->assertNull(optional((object) ['foo' => 'bar'])->bar);

    }



    public function 
testOptionalDeterminesWhetherKeyIsSet()
    public function testOptionalDeterminesWhetherKeyIsSet()

    {

        
$this->assertTrue(isset(optional(['foo' => 'bar'])['foo']));

        
$this->assertFalse(isset(optional(['foo' => 'bar'])['bar']));

        
$this->assertFalse(isset(optional()['bar']));

    }
    public function testOptionalDeterminesWhetherKeyIsSet()

    {

        
$this->assertTrue(isset(optional(['foo' => 'bar'])['foo']));

        
$this->assertFalse(isset(optional(['foo' => 'bar'])['bar']));

        
$this->assertFalse(isset(optional()['bar']));

    }
    {

        
$this->assertTrue(isset(optional(['foo' => 'bar'])['foo']));

        
$this->assertFalse(isset(optional(['foo' => 'bar'])['bar']));

        
$this->assertFalse(isset(optional()['bar']));

    }



    public function 
testOptionalAllowsToSetKey()