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 return [
'title' => $this->title(),
'path' => $this->path(),
'parent_path' => optional($this->parent())->path(),
'basename' => $this->basename(),
];
}
<?php protected function searchIndex()
{
return optional($this->data->searchIndex())->name();
}
protected function assets()
<?php public function avatarFieldUrl()
{
return optional($this->avatarFieldValue()->value())->url();
}
<?php private function isOnAllowedRoute()
{
if (! $route = optional(request()->route())->getName()) {
return false;
}
<?php public function orderable()
{
return optional($this->structure())->maxDepth() === 1;
}
public function sortField($field = null)
<?php $site = $site ?? $this->sites()->first();
return optional($mount->in($site))->url();
}
public function uri($site = null)
<?php $site = $site ?? $this->sites()->first();
return optional($mount->in($site))->uri();
}
public function showUrl()
<?php private function dispatchEntryBlueprintFoundEvent($blueprint, $entry)
{
$id = optional($entry)->id() ?? 'null';
$blink = 'collection-entry-blueprint-'.$this->handle().'-'.$blueprint->handle().'-'.$id;
<?php 'locale' => $this->locale(),
'origin' => $this->hasOrigin() ? $this->origin()->id() : null,
'slug' => $this->slug(),
'date' => optional($this->date())->format('Y-m-d-Hi'),
'published' => $this->published(),
'path' => $this->initialPath() ?? $this->path(),
'data' => $this->data(),
<?php tap($structure->in($this->locale()), function ($tree) {
$parent = $this->parent();
if (optional($parent)->isRoot()) {
$parent = null;
}
$this->page()->pages()->all()->each(function ($child) use ($tree, $parent) {
<?php $parent = null;
}
$this->page()->pages()->all()->each(function ($child) use ($tree, $parent) {
$tree->move($child->id(), optional($parent)->id());
});
$tree->remove($this);
})->save();
<?php $this->taxonomize();
optional(Collection::findByMount($this))->updateEntryUris();
foreach ($afterSaveCallbacks as $callback) {
$callback($this);
<?php return $this
->fluentlyGetOrSet('template')
->getter(function ($template) {
return $template ?? optional($this->origin())->template() ?? $this->collection()->template();
})
->args(func_get_args());
}
<?php return $this
->fluentlyGetOrSet('layout')
->getter(function ($layout) {
return $layout ?? optional($this->origin())->layout() ?? $this->collection()->layout();
})
->args(func_get_args());
}
<?php {
$array = Arr::removeNullValues([
'id' => $this->id(),
'origin' => optional($this->origin())->id(),
'published' => $this->published === false ? false : null,
'blueprint' => $this->blueprint()->handle(),
]);