Error null en relaciones

Operador coalescing:

$username = $task->user->name ?? 'no existe';

Operador null_safe:

$username = $task?->user?->name? ;

Mediante optional:

$username = optional($task->user)->name;

Request form:

public function user(): BelongsTo
{
return $this->belongsTo(User::class)->withDefault(

['name' => 'Sin usuario']
);
}

Deja un comentario